BDD测试框架Cucumber学习

📋 个人简介

  •  作者简介:大家好,我是凝小飞,软件测试领域作者
  • 支持我:点赞👍+收藏⭐️+留言📝

Cucumber是一个行动驱动开发(BDD)的测试框架,它支持多种编程语言,包括Java、Python、Ruby等。主要特点是用自然语言来描述测试用例,这样非技术人员也能够理解参与测试过程。我认为,未来可以跟AI结合,完成从测试用例编写到自动生成的过程。国外的论坛和活跃度,更新度还挺高的。

目前我在从事金融业务的自动化测试,用的是cucumber的框架,正在边学边做中。会持续的更新。

以下是举一个例子是基于Selenium、Cucumber和Python3的登录页面UI自动化脚本:

首先,你需要安装所需的依赖包。你可以使用以下命令安装它们:

pip install selenium pip install behave

接下来,创建一个.feature文件,用于定义Cucumber的场景和步骤。在这个文件中,你可以描述登录页面的各种场景和预期结果。例如,创建一个名为login.feature的文件,内容如下:

Feature: Login As a user I want to be able to login to the website

Scenario: Successful login

Given I am on the login page

When I enter my username and password And I click the login button

Then I should be redirected to the home page

Scenario: Failed login

Given I am on the login page

When I enter invalid username or password And I click the login button

Then I should see an error message

接下来,创建一个.steps文件,用于实现Cucumber的步骤定义和与Selenium交互的代码。创建一个名为login_steps.py的文件,内容如下:

from behave import given, when, then 
from selenium import webdriver 

@given('I am on the login page') 
def step_impl(context): 
context.driver = webdriver.Chrome() 
context.driver.get('http://example.com/login') 

@when('I enter my username and password') 
def step_impl(context): 
username_input = context.driver.find_element_by_id('username') 
password_input = context.driver.find_element_by_id('password') username_input.send_keys('my_username') 
password_input.send_keys('my_password') 

@when('I enter invalid username or password') 
def step_impl(context): 
username_input = context.driver.find_element_by_id('username') 
password_input = context.driver.find_element_by_id('password') username_input.send_keys('invalid_username') password_input.send_keys('invalid_password') 


@when('I click the login button') 
def step_impl(context): 
login_button = context.driver.find_element_by_id('login_button') login_button.click() 

@then('I should be redirected to the home page') 
def step_impl(context): 
assert context.driver.current_url == 'http://example.com/home' 

@then('I should see an error message') 
def step_impl(context): 
error_message = context.driver.find_element_by_id('error_message') 
assert error_message.is_displayed()

最后,创建一个运行测试的文件。创建一个名为run_tests.py的文件,内容如下:

from behave import __main__ as behave_executable 
if __name__ == '__main__': 
behave_executable.main(args=['--no-capture', 'features'])

现在你可以在命令行中运行测试了。只需执行以下命令:

python run_tests.py

这将运行Cucumber测试并使用Selenium执行UI自动化。

当然,以上只是最简单的一个例子。要深入了解cucumber,还得去继续了解Scenario Outline、Feature 文件参数化、Hook、测试报告的使用等等,有空我会再后续文章中展开。

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-10 00:22:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-10 00:22:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-10 00:22:01       82 阅读
  4. Python语言-面向对象

    2024-03-10 00:22:01       91 阅读

热门阅读

  1. C# 的一些好用的语法糖介绍

    2024-03-10 00:22:01       41 阅读
  2. linux脚本练习2-文件压缩删除

    2024-03-10 00:22:01       43 阅读
  3. 铭文资产是比特币生态破局者 or 短暂热点?

    2024-03-10 00:22:01       36 阅读
  4. 数据结构-二分查找

    2024-03-10 00:22:01       47 阅读
  5. 如何更好的理解设计模式之桥接模式

    2024-03-10 00:22:01       45 阅读
  6. golang从0到1实战系统四十:处理表单的输入

    2024-03-10 00:22:01       44 阅读
  7. YUNBBE云贝:PG表空间介绍

    2024-03-10 00:22:01       48 阅读
  8. Promise介绍

    2024-03-10 00:22:01       49 阅读
  9. LeetCode168. Excel Sheet Column Title

    2024-03-10 00:22:01       45 阅读
  10. mysql与oracle的区别

    2024-03-10 00:22:01       40 阅读
  11. HTTPS运行加密的过程

    2024-03-10 00:22:01       48 阅读