• pytest(二)--用例运行规则


    用例设计原则

      文件名以test_*.py文件和*_test.py

      以test_开头的函数

      以Test开头的类

      所有的包pakeage必须要有__init__.py文件

    help帮助

    1.查看pytest命令行参数,可以用pytest -h 或 pytest --help查看

     

     可以按照如下写用例

    #D:Python08112020	est_0728
    #test_class.py
    class TestClass:
        def test_onea(self):
            x="this"
            assert 'h' in x
        def test_twob(self):
            x="hello"
            assert hasattr(x,'check')
        def test_threec(self):
            a="hello"
            b="hello world"
            assert a in b
    
    #test.sa.py
    def func(x):
        return x+1
    def test_answer():
        assert func(3)==5
    

       

     cmd下执行pytest用例的三种方法

      pytest (推荐)

      py.test

      python -m pytest

    如果不带参数,在某个文件夹下执行时,它会查找该文件夹下所有的符合条件的用例(查看用例设计原则)

    执行用例规则

    1.执行某个目录下所有的用例

      pytest 文件名/

    2.执行某一个py文件下用例

      pytest 脚本名称.py

    3.-k按关键字匹配

      pytest -k "MyClass and not method"

    这将运行包含与给定字符串表达式匹配的名称的测试,其中包括Python
    使用文件名,类名和函数名作为变量的运算符。

    如,pytest -k "test_class.py"

    哪些不执行,还是不明白

    4.按节点运行

      每个收集的测试都分配了一个唯一的nodeid,它有模块文件名和后跟说明符组成来自参数化的类名,函数名和参数,有::分割。

    运行.py模块里面的某个函数

    pytest test_mod.py::test_func;如,pytest test_sa.py::test_answer

    运行.py模块里面,测试类里面的某个方法

    pytest test_mod.py::TestClass::test_method;

    如,pytest test_class.py::TestClass::test_twob

    5.标记表达式

    pytest -m slow

    将运行用@pytest.mark.slow装饰器修饰的所有测试。

    6.从包里面运行

    pytest --pyargs pkg.testing

    这将导入pkg.testing并使用某文件系统位置来查找和运行测试。

    -x 遇到错误时停止测试

    pytest -x test_class.py

    从运行结果可以看出,本来有3个用例,第二个用例失败后不再继续往下执行了

     --maxfail=num

    pytest --maxfail=1

    当用例错误个数达到指定数量时,停止测试

    越努力,越幸运!!! good good study,day day up!!!
  • 相关阅读:
    [转]UNI-APP开发笔记之使用uni.navigateBack修改上一个页面值
    [转]移动端人员选择的设计思考
    [转]nginx安装及其配置详细教程
    [转]Vue 使用use、prototype自定义自己的全局组件
    [转]uniapp项目运行支付宝小程序,报错:xxx.json中没有申明component: true
    支付宝(钉钉)小程序使用uView控制台报错Cannot read property 'title-all' of undefined
    [转]commonJS规范和require,import区别
    [转]module.exports和export详解
    [转]如何在组件中去使用vuex的值和方法?
    [转]CryptoJS中AES256(CBC)加密算法简单使用
  • 原文地址:https://www.cnblogs.com/canglongdao/p/13392348.html
Copyright © 2020-2023  润新知