python系列之初探Pytest(2)-运行方式

​   Pytest的运行方式有多种,可以支持单个测试用例执行、分组执行、文件执行、目录执行等,同时还执行多线程执行。

 

单个用例执行:

    pytest 模块名.py::[测试类名]::[测试方法名]

 

示例:

pytest test_pytestDemo.py::TestDemoAAAA::test_DemoA1 -s

 

执行测试类里面的所有用例:

    pytest 模块名.py::[测试类名]

 

示例:

pytest test_pytestDemo.py::TestDemoAAAA -s

 

执行单个文件中的所有用例:

    pytest 文件名

示例:

pytest test_pytestDemo.py -s

 

执行目录下的所有用例:

   pytest 文件目录

示例:

pytest testcase/

 

分组执行:

   可以通过pytest.mark 修饰符自定义标识名称进行分组。

   pytest [模块名.py] -m 标识名

 

示例:

pytest test_pytestDemo.py -m L1 -s

Mark自定义名称需要在目录下创建conftest.py 配置文件,pytest会自动去找里面的配置内容。

代码如下:

注意pytest_configure方法名不能变。

def pytest_configure(config):    config.addinivalue_line("markers","L1")    config.addinivalue_line("markers", "L2")    config.addinivalue_line("markers", "L3")    config.addinivalue_line("markers", "L4")

 

多线程执行:

  这个就需要用到第三方插件:pytest-xdist

    pip install -U pytest-xdist

使用方法:

   pytest 模块名 -n num

   num:表示要开启的线程数。

示例:

pytest test_pytestDemo.py -n 3

  多线程下好像不支持 -s 的print输出

 

测试代码:

#pytest test_pytestDemo.pyimport pytest@pytest.mark.L1class TestDemoAAAA:    def test_DemoA1(self):        print("this is a pytest Demo A11111111111")        assert True    def test_DemoA2(self):        print("this is a pytest Demo A222222222222")        assert True    def test_DemoA3(self):        print("this is a pytest Demo A33333333333")        assert True@pytest.mark.L2class TestDemoBBBB:    def test_DemoB1(self):     print("this is a pytest Demo B111111111")     assert True    def test_DemoB2(self):     print("this is a pytest Demo B222222")     assert True    def test_DemoB3(self):     print("this is a pytest Demo B33333")     assert True@pytest.mark.L3def test_DemoB3():    print("this is a pytest Demo B33333")    assert Trueif __name__ == '__main__':    pytest.main([("test_pytestDemo.py"),("-s")])

 

-------------------------------------最后---------------------------------

 更多软件测试相关内容请关注“软件测试道与术”公众号或扫描下方二维码

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值