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

    负重前行
  • 相关阅读:
    HDU 1525
    kmp模板
    hdu 4616 Game(树形DP)
    hdu 4619 Warm up 2(并查集活用)
    hdu 4614 Vases and Flowers(线段树加二分查找)
    Codeforces 400D Dima and Bacteria(并查集最短路)
    poj 2823 Sliding Window (单调队列)
    hdu 2196 Computer(树形dp)
    hdu 4604 Deque
    最短路径
  • 原文地址:https://www.cnblogs.com/astride/p/12835152.html
Copyright © 2011-2022 走看看