• 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))
    ------------------------- A little Progress a day makes you a big success... ----------------------------
  • 相关阅读:
    JS解析JSON 注意事项总结
    Jquery 绑定标签事件
    System.Globalization.CultureInfo.InvariantCulture 解决不同地域字符串格式不同问题
    c# HttpWebRequest 模拟HTTP post 传递JSON参数
    Django REST framework (DRF) 不能用property或method排序
    django filters TypeError __init__() got an unexpected keyword argument 'lookup_type'
    DRF(Django Rest Framework)备忘
    测试服务器
    还款计算-复式记账
    Django 相关内容blog备忘
  • 原文地址:https://www.cnblogs.com/qianjinyan/p/14469767.html
Copyright © 2020-2023  润新知