标签: python-behave

使用behave(python)定义场景(或所需场景)的顺序

我正在使用表现来测试我的小Django应用程序.

我已经创建了user_management.feature包含此场景的文件:

Scenario: register
 Given I can access registration form
  When I put "doctor" in "username" field
   And I put "tardisBlue" in "password" field
   And I put "doctor@tardis.com" in "email" field
   And I press the "Register" button
  Then the registration is successful
   And I am logged in
Run Code Online (Sandbox Code Playgroud)

Everythig工作正常.

我想要开发的下一个功能是在文件中project_management.feature:

Scenario: create a project
  Given I am logged in
  When I go to the home page
   And I click on "Create new Project" link
   And I fill …
Run Code Online (Sandbox Code Playgroud)

python tdd bdd python-behave

8
推荐指数
1
解决办法
3785
查看次数

用于行为测试框架的测试覆盖工具

我们使用Behave BDD工具自动化API.是否有任何工具使用我们的行为案例来提供代码覆盖?

我们尝试使用覆盖模块,它不适用于Behave.

python automated-tests coverage.py python-behave

7
推荐指数
1
解决办法
2781
查看次数

将常用属性添加到Behave方法

使用伟大的Behave框架,但我缺乏OOP技能.

Behave有一个内置的上下文命名空间,可以在测试执行步骤之间共享对象.在初始化我的WebDriver会话之后,我继续在我的步骤之间传递它来使用它context来保存所有内容.功能很好,但正如你在下面看到的那样,除了DRY之外什么都不是.

如何/在哪里可以将这些属性添加到step_impl()context仅一次?

environment.py

from selenium import webdriver

def before_feature(context, scenario):
    """Initialize WebDriver instance"""

    driver = webdriver.PhantomJS(service_args=service_args, desired_capabilities=dcap)

    """
    Do my login thing..
    """

    context.driver = driver
    context.wait = wait
    context.expected_conditions = expected_conditions
    context.xenv = env_data
Run Code Online (Sandbox Code Playgroud)

steps.py

@given('that I have opened the blah page')
def step_impl(context):

    driver = context.driver
    wait = context.wait
    expected_conditions = context.expected_conditions
    xenv = context.xenv

    driver.get("http://domain.com")
    driver.find_element_by_link_text("blah").click()
    wait.until(expected_conditions.title_contains("Blah page"))

@given(u'am on the yada subpage')
def step_impl(context):
    driver = context.driver …
Run Code Online (Sandbox Code Playgroud)

python oop python-behave

7
推荐指数
1
解决办法
4025
查看次数

在行为python中定义上下文变量

有时,您需要动态定义值(如现在的datetime,随机字符串,随机整数,文件内容等),并在不同的步骤中使用它们,而不是显式或硬编码值.

所以,我的问题是如何在步骤中定义变量(正确的方法)在以下步骤中使用这些变量.

一些例子

Given A random string of length "100" as "my_text"
And I log in to my platform
And I ask to add the following post:
 | title                    | description |
 | Some example of title    | {{my_text}} |
When I submit the post form
Then The posts table shows these posts:
 | title                    | description |
 | Some example of title    | {{my_text}} |
And I delete any post containing in the description "{{my_text}}"
Run Code Online (Sandbox Code Playgroud)

这是一个基本的例子,试图解释为什么我想在步骤中定义变量并将它们保存在上下文中以便在以下步骤中使用它.

我的想法是修改before_step和after_step方法......在上下文中设置一个变量来存储我的自定义变量,如下所示:

def before_step(context): …
Run Code Online (Sandbox Code Playgroud)

python testing contextmanager python-behave

7
推荐指数
1
解决办法
2695
查看次数

如何跳过行为python BDD框架中的测试?

我正在处理几个月前部分完成的代码分支,它们依赖于交织在一起.因此,向前推进的最简单方法是将特定分支上的失败测试标记为挂起(rspec方式)或跳过,并在所有内容合并后处理它们.

在最终报告中,behave报告已通过的测试数量,#failed,#skipped和#untested(当我按Ctrl-C中止运行时,它们不为零).所以behave作为跳过测试的概念.我该如何访问?

bdd python-behave

7
推荐指数
2
解决办法
6512
查看次数

记录未在行为步骤中捕获

好吧,在我的environment.py文件中,我可以通过以下方式记录东西:

logging.basicConfig(level=logging.DEBUG, filename="example.log")

def before_feature(context, feature):
    logging.info("test logging")
Run Code Online (Sandbox Code Playgroud)

但当我在步骤文件中时,我无法执行日志记录:

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

@given("we have a step")
def step_impl(context):
    logger.debug("Test logging 2")
Run Code Online (Sandbox Code Playgroud)

步骤内的日志消息不会显示.我正在使用python行为模块.有任何想法吗?

我在运行行为时尝试启用和禁用logcapture,但它没有任何区别.

python bdd logging python-behave

7
推荐指数
2
解决办法
1102
查看次数

行为:如何从另一个文件导入步骤?

我刚刚开始使用behave,一个使用Gherkin 语法的Pythonic BDD 框架。行为需要一个功能,例如:

Scenario: Calling the metadata API
   Given A matching server
   When I call metadata
   Then metadata response is JSON
   And response status code is 200
Run Code Online (Sandbox Code Playgroud)

还有一个步骤文件,例如:

...
@then('response status code is {expected_status_code}')
def step_impl(context, expected_status_code):
    assert_equals(context.response.status_code, int(expected_status_code))

@then('metadata response is JSON')
def step_impl(context):
    json.loads(context.metadata_response.data)
...
Run Code Online (Sandbox Code Playgroud)

并将它们组合成一个漂亮的测试报告:

检测结果

其中一些步骤 - 例如:

  • metadata response is JSON
  • response status code is {expected_status_code}

在我的许多项目中使用,我想将它们分组到一个通用步骤文件中,我可以导入和重用该文件。

我尝试将有用的步骤提取到单独的文件中并将其导入,但收到以下错误:

@then('response status code is {expected_status_code}')
NameError: name 'then' is not defined
Run Code Online (Sandbox Code Playgroud)

如何创建通用步骤文件并导入它?

python bdd python-behave

6
推荐指数
2
解决办法
4130
查看次数

使用 Pycharm 调试 Python 行为步骤

我正在使用 Pycharm 编写测试并以行为方式运行它们。我正在使用 cli 运行行为命令。为了编写功能和场景,我正在使用 Pycharm。我如何调试每个步骤?

python pycharm python-behave

6
推荐指数
1
解决办法
3038
查看次数

如何在 BDD 步骤文件中进行模拟

我想模拟 os.path.exists 方法的行为,以便在 os.path.exists 报告文件/文件夹不存在时验证我的脚本是否行为正确。

@when("Service starts with input file that does not exist")
def step_impl(context):
    """
    :type context: behave.runner.Context
    """
    json_file_path = "fake_file_path"
    mock_os_path = mock.Mock()
    mock_os_path.exists.return_value = False

    context.returncode = dicom_send_service.launch(json_file_path)

    mock_os_path.exists.assert_called_once_with(json_file_abspath)
Run Code Online (Sandbox Code Playgroud)

如何将模拟注入到我的脚本中?我尝试使用

@mock.patch("mymodule.os.path")
@when("Service starts with input file that does not exist")
def step_impl(context, mock_os_path):
Run Code Online (Sandbox Code Playgroud)

但是,当我运行该方法时,python 返回:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/behave/model.py", line 1456, in run
    match.run(runner.context)
  File "/usr/local/lib/python2.7/dist-packages/behave/model.py", line 1903, in run
    self.func(context, *args, **kwargs)
TypeError: step_impl() takes exactly 2 arguments (1 given)
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,step_impl …

python bdd mocking python-behave

6
推荐指数
1
解决办法
3873
查看次数

KeyError:从本地目录导入时发生“'__name__' not in globals”

目录结构:

app\features\login.feature
app\features\__init__.py
app\features\steps\__init__.py
app\features\steps\login.py
app\fixtures\__init__.py
app\fixtures\fixtures.py
app\models\__init__.py
app\models\login.py
Run Code Online (Sandbox Code Playgroud)

这是我的文件里面的内容。当我在from ...models.login import Login

from behave import *
from ...models.login import Login
from ...models.footer import Footer

@given("unauthenticated user loads the home page")
def step_impl(context):
    assert Login.is_sign_in_submit_displayed(context, Login.signinbutton)

@step("sign-in button is clicked")
def step_impl(context, signinbutton):
    Login.tap_sign_in(context, Login.signinbutton)

@when("login form is populated with valid credentials")
def step_impl(context):
    Login.enter_email(context, Login.email, DEFAULT_EMAIL)
    Login.enter_password(context, Login.password, DEFAULT_PASSWORD)

@then("login is successful")
def step_impl(context):
    assert Login.home_page.is_rewards_displayed(context, Footer.menubutton)
    assert Login.home_page.is_account_displayed(context, Footer.accountbutton
Run Code Online (Sandbox Code Playgroud)

这是它在 app\models\login.py 中找不到的文件:

class Login():
    """Login screen object …
Run Code Online (Sandbox Code Playgroud)

python python-behave

6
推荐指数
1
解决办法
4461
查看次数