pytest-BDD 的学习

https://pytest-bdd.readthedocs.io/en/latest/#bdd-library-for-the-py-test-runner

BDD library for the py.test runner

pytest-bdd implements a subset of the Gherkin language to enable automating project requirements testing and to facilitate behavioral driven development.

pytest-bdd实现了Gherkin语言的子集,以实现自动化项目需求测试并促进行为驱动的开发。

 

Unlike many other BDD tools, it does not require a separate runner and benefits from the power and flexibility of pytest. It enables unifying unit and functional tests, reduces the burden of continuous integration server configuration and allows the reuse of test setups.

与许多其他BDD工具不同,它不需要单独的运行程序,并且可以从pytest的功能和灵活性中受益。 它可以统一单元测试和功能测试,减轻连续集成服务器配置的负担,并允许重用测试设置。

 

Pytest fixtures written for unit tests can be reused for setup and actions mentioned in feature steps with dependency injection. This allows a true BDD just-enough specification of the requirements without maintaining any context object containing the side effects of Gherkin imperative declarations.

为单元测试编写的Pytest固定装置可以通过依赖项注入重新用于功能步骤中提到的设置和操作。 这允许对需求进行真正的BDD正当说明,而无需维护任何包含Gherkin命令性声明的副作用的上下文对象。

Install pytest-bdd

pip install pytest-bdd

  

Example

Feature: stage 01 This is a test

  Scenario: This is a test sample
    Given I have a book and its name is learn
    And I have finished 20 pages
    When I begin to read the 21 page
    And I read 10 pages
    Then I verify the next time I should read from Page 31

 

Given 是前置条件, previous condition

when 是进行的操作,steps to do

then 是进行验证, 期望值

但是并不是一个简单的方法来进行验证,尤其在UI自动化的时候,页面本身能不能点,能不能跳转,元素在不在,既是进行的操作,本身也是验证  

 

 

以下是每一个step,这些方法没有具体实现,只是进行了打印。

from time import sleep

import pytest
from pytest_bdd import when, then, parsers
from pytest_bdd import scenario, given


###############
# STEPS #
###############
@given('I have a book and its name is learn')
def step_click_my_audience_page():
    print('I have a book and its name is learn')


@given('I have finished 20 pages')
def book_page_i_have_a_book():
    print("hello yes i have a book and its name is {}".format("book"))


@when('I begin to read the 21 page')
def book_i():
    print('I begin to read the 21 page')


@when('I read 10 pages')
def book_2():
    print('I read 10 pages')


@then('I verify the next time I should read from Page 31')
def book333():
    print('I verify the next time I should read from Page 31')


@given(parsers.cfparse('case {num}'))
def book_seq(num):
    print('Hello there,I am reading book page {}'.format(num))

 

posted @ 2021-03-02 16:46  巴黎爱工作  阅读(1103)  评论(0编辑  收藏  举报