zoukankan      html  css  js  c++  java
  • pytest初始化清除与挑选用例执行

    pytest

    # pytest 命令行参数
    pytest cases
    
    # 输出打印结果
    pytest cases -s
    
    # 输出更详细信息
    pytest cases -sv
    
    # 打印报告
    pytest cases --html=报告名称.html --self-contained-html   (--self-contained-html 此参数为不生成css目录)
    pytest cases --html=report.html --self-contained-html
    # C:PythonPython37Libsite-packagespytest_htmlplugin.py文件 解决报告乱码
    
    初始化清除
    • 模块级别

      def setup_module():
          print('
       *** 初始化-模块 ***')
      
      
      def teardown_module():
          print('
       ***   清除-模块 ***')
      
    • 类级别

      class Test_错误密码:
      
          @classmethod
          def setup_class(cls):
              print('
       === 初始化-类 ===')
      
          @classmethod
          def teardown_class(cls):
              print('
       === 清除 - 类 ===')
      
    • 方法级别

      class Test_错误密码:
      	def setup_method(self):
              print('
       --- 初始化-方法  ---')
      
          def teardown_method(self):
              print('
       --- 清除  -方法 ---')
      
    • 目录级别

      我们在需要初始化的目录下面创建 一个名为 conftest.py 的文件,里面内容如下所示

      import pytest 
      
      @pytest.fixture(scope='package',autouse=True)
      def st_emptyEnv():
          print(f'
      #### 初始化-目录甲')
          yield
          
          print(f'
      #### 清除-目录甲')
      
    挑选用例执行
    # 根据需求逐级递减
    pytest cases登录	est_错误登录.py::Test_错误密码::test_C001001
    
    • 根据名字

      # pytest -k 名字 -sv
      pytest -k C001001 -s
      pytest -k "not C001001" -s
      pytest -k "错 and 密码2" -s
      pytest -k "错 or 密码2" -s
      
    • 根据标签

      如果是要统一执行散落的一些测试用例,可以选择加标签,可以给方法或类,以及整个文件添加标签

      import pytest
      
      class Test_错误密码2:
          @pytest.mark.标签名      # <------标签
          def test_C001021(self):
              print('
      用例C001021')
              assert 1 == 1
      
      pytest cases -m 标签名 -s    # <------命令行执行执行
      

      可以这样定义一个全局变量 pytestmark 为 整个模块文件 设定标签

      import pytest
      pytestmark = pytest.mark.网页测试
      

      如果你需要定义多个标签,可以定义一个列表

      import pytest
      pytestmark = [pytest.mark.网页测试, pytest.mark.登录测试]
      
    种一棵树最好的时间是十年前,其次是现在
  • 相关阅读:
    [LeetCode] 771. Jewels and Stones
    [LeetCode] 129. Sum Root to Leaf Numbers
    java定时器demo
    Spring Boot与监控管理
    springboot与热部署
    springboot中的web项目不能访问templates中的静态资源
    @Component 和 @Bean 的区别
    springcluoud入门
    Dubbo和Zookerper的关系
    Spring boot配置Dubbo三种方式
  • 原文地址:https://www.cnblogs.com/fairytalk/p/13407393.html
Copyright © 2011-2022 走看看