• 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

    负重前行
  • 相关阅读:
    Android 最火框架XUtils之注解机制具体解释
    Oracle GoldenGate从oracle db 到非oracle db的初始化数据同步的方法
    Java中接口和抽象类的比較
    spring+springmvc+hibernate架构、maven分模块开发样例小项目案例
    配置Java连接池的两种方式:tomcat方式以及spring方式
    Ant报错之out of memory
    Mybatis 框架文档 超具体笔记
    jsp
    HDU 1251 统计难题(字典树)
    HDU 1251 统计难题(字典树)
  • 原文地址:https://www.cnblogs.com/astride/p/12835152.html
Copyright © 2020-2023  润新知