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

    负重前行
  • 相关阅读:
    静态(static)代码块、构造代码块、构造函数、父类子类执行顺序
    Java基本特征
    下列哪项不属于jdk1.6垃圾收集器?
    Model-View-Controller(MVC) is an architectural pattern that frequently used in web applications. Which of the following statement(s) is(are) correct?
    ServletConfig对象详解
    ServletConfig接口默认是哪里实现的?
    eclipse根据父类打开子类快捷键
    IDE:Eclipse查看Servlet源码
    IDE:Eclipse查看接口实现类快捷键
    Qt探索之路——多线程实现方法
  • 原文地址:https://www.cnblogs.com/astride/p/12835152.html
Copyright © 2011-2022 走看看