zoukankan      html  css  js  c++  java
  • pytest高级用法

    1、自定义marks

    测试场景:

    1. 在大厂里面,一个系统的测试用例好几千个,每次发版都执行来不及
    2. 通过关键字来运行测试用例,pytest -k,    缺陷明显:无法多重打标,难于维护
    3. 通过标签来运行测试用例,既可以作用于函数又可以作用于类

    # pytest.init

    [pytest]

    markers = 

    webtest:   Run the webtest case

    hello:    Run the hello case

    P0:   Run P0 case

    P1:  Run P1 case

    2、fixtures

    fixtures的定义:为了测试用例的执行,而初始化一些数据和方法

    1. 类似于unittest里面的setUp和tearDown
    2. pytest fixtures调用起来 比unittest里面的setUp和tearDown方便
    3. 支持不同级别的fixtures (session,class,function)  
    4. 调用更加灵活,支持直接函数名调用,decorator调用,autouse

    3、fixtures的三种调用方法

    • 直接通过函数名字调用
    • 使用usefixtures    decorator
    • 使用autouse

      import pytest

      @pytest.fixture()

      def loginandlogout()

        print("/ndo login action ") 

        yield     ##(测试失败的时候截屏)

        print("do logout action ")

      class TestSample:

        @pytest.mark.usefixtures('loginandlogout')  ###注意写法

        def test_answer(self.loginandlogout)

          print("I am test answer ..... ")

          assert 1+9==10

        

        @pytest.mark.usefixtures('loginandlogout') 

        def test_answer2(self.loginandlogout)

          print("I am test answer2 ..... ")

          assert 1+10==11

    ——————————————————————————————————————————————————————————————

    第二种方法:

      import pytest

      @pytest.fixture(scope='module',autouse=True)

      def loginandlogout()

        print("/ndo login action ") 

        yield     ##(测试失败的时候截屏)

        print("do logout action ")

      

      @pytest.fixture(scope='module',autouse=True)

      def clickhome()

        print("click  home button ") 

        yield     ##(测试失败的时候截屏)

        print("end home link ")

      

      class TestSample:

        def test_answer(self.loginandlogout)

          print("I am test answer ..... ")

          assert 1+9==10

        

        def test_answer2(self.loginandlogout)

          print("I am test answer2 ..... ")

    pytest 高级用法conftest.py

    conftest.py文件中定义共享的fixture

    conftest.py一般放在testcase的目录下面,每个目录下也存在着conftest.py

    如果子目录下有conftest.py,子目录下的conftest.py中的fixture优先

          assert 1+10==11

    负重前行
  • 相关阅读:
    新词发现
    隐马尔可夫模型
    nodejs命令行解析工具之minimist
    react 生命周期
    react 中的 super super(props)
    object 常用方法总结
    前端安全 中间人攻击
    Postgresql的临时表的用法
    windows下postgresql数据库备份和还原
    PostgreSQL 数据库开发规范——命名规范 & 设计规范
  • 原文地址:https://www.cnblogs.com/astride/p/12835152.html
Copyright © 2011-2022 走看看