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、测试报告的使用等等,有空我会再后续文章中展开。

  • 13
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凝小飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值