zl程序教程

您现在的位置是:首页 >  Python

当前栏目

pytest.main( )参数大全和装饰器

2023-03-14 09:51:13 时间

一、常用参数大全

1、-s:输出打印(print/logging)信息;

2、-v:显示具体丰富的信息;

3、-k:执行用例名称中包含某字符段的用例。

  举个 pytest.main(['-k','taiwan']) #执行测试用例名称包含taiwan的用例;

    pytest.main(['-k',"One",'./testOne/test_one.py'])#匹配test_one.py模块下包含One的用例

4、-q:输出简化信息,不输出环境信息;

5、-x:只要出现测试用例失败,后面的用例不再执行;

6、--junit-xml:设置junit xml测试报告。  

  举个  pytest.main(['--junit-xml=./report/junit_report.xml']);

7、--maxfail:设置失败几次就结束测试。

  举个  pytest.main(['--maxfail=3']) #三次失败后结束所有测试;      

 二、选择执行用例

pytest.main(['test_suit.py::test_case_name']);#精确选择

pytest.main(['./']) #运行./目录下所有(test_*.py和*_test.py);

pytest.main(['./testOne']) #运行./testOne目录下用例;

pytest.main(['./testOne/test_one.py']) #运行指定模块;

pytest.main(['-k','order','./testOne/test_one.py::TestOne']) #匹配TestOne类中包含order的用例

三、添加装饰器

1)首先配置文件.ini文件,设置基础参数:
 [pytest]
 markers =
     标签名: 描述信息
     slow: mark tests as slow
     smoke: mark tests as smoke            
 2)在测试用例添加标记装饰器@pytest.mark.slow(slow是标签名)

@pytest.mark.skip(reason=跳过用例的原因)

@pytest.mark.skipif(跳过用例的条件)

@pytest.mark.xfail(condition=预期失败条件,reason=预期失败的原因)

@pytest.mark.flaky(reruns=最多重跑次数, reruns_delay=失败后延时时间)

@pytest.mark.parametrize(value,[1,2,3])

1 @pytest.fixture()
2 def func():
3     a = 'leo'
4     return a
5 
6 def test_001(func):
7     assert func == 'leo1'

 3)main函数写作:pytest.main(['-m', 'slow'])

-m支持and、or、not,  举个 pytest.main(['-m smoke and not slow'])

四、多线程执行用例

1、安装 python3 -m pip install pytest-xdist

2、运行 -n 3(指定并行数):pytest -v -n 2

 

 

 

很多时候,我们在听别人讲话的时候,其实总是急于表达自己的观点,迫切的想要表达出来好得到对方的认可。而有时候说出口的话不经大脑思考,脱口而出,事后便会发现那句话说得有问题,在无意间还是伤害到了对方的面子,这是隐形的暴力沟通。好好说话看似很简单,其实很难,这是一门学问,还需精心修炼!