首先来看什么参数都没加的运行情况
- class TestClass():
- def test_zne(self):
- print(1)
- assert 1==2
-
- def test_two(self):
- print(2)
- assert 1==2
-
- def test_a(self):
- print(3)
- assert 1==1
-
- if __name__ == '__main__':
- pytest.main()
-
- ============================= test session starts =============================
- platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
- rootdir: D:\Users\72036454\Desktop\pythonProject\Base
- plugins: allure-pytest-2.9.45
- collected 3 items
-
- test_page.py FF. [100%]
-
- ================================== FAILURES ===================================
用于打印显示每条用例的执行情况
- import pytest
- class TestClass():
- def test_zne(self):
- print(1)
- assert 1==2
-
- def test_two(self):
- print(2)
- assert 1==2
-
- def test_a(self):
- print(3)
- assert 1==1
-
- if __name__ == '__main__':
- pytest.main(['-v'])
-
- ============================= test session starts =============================
- platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- D:\Users\72036454\AppData\Local\Programs\Python\Python38\python.exe
- cachedir: .pytest_cache
- rootdir: D:\Users\72036454\Desktop\pythonProject\Base
- plugins: allure-pytest-2.9.45
- collecting ... collected 3 items
-
- test_page.py::TestClass::test_zne FAILED [ 33%]
- test_page.py::TestClass::test_two FAILED [ 66%]
- test_page.py::TestClass::test_a PASSED [100%]
-
- ================================== FAILURES ===================================
- 现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
- 如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
- 可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
- 分享他们的经验,还会分享很多直播讲座和技术沙龙
- 可以免费学习!划重点!开源的!!!
- qq群号:110685036

简化测试整体结果。F:代表测试失败、.:代表测试通过
- import pytest
- class TestClass():
- def test_zne(self):
- print(1)
- assert 1==2
-
- def test_two(self):
- print(2)
- assert 1==2
-
- def test_a(self):
- print(3)
- assert 1==1
-
- if __name__ == '__main__':
- pytest.main(['-q'])
-
- FF. [100%]
- ================================== FAILURES ===================================
显示测试用例中 print() 中的值
- import pytest
- class TestClass():
- def test_zne(self):
- print(1)
- assert 1==2
-
- def test_two(self):
- print(2)
- assert 1==2
-
- def test_a(self):
- print(3)
- assert 1==1
-
- if __name__ == '__main__':
- pytest.main(['-s'])
-
- ============================= test session starts =============================
- platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
- rootdir: D:\Users\72036454\Desktop\pythonProject\Base
- plugins: allure-pytest-2.9.45
- collected 3 items
-
- test_page.py 1
- F2
- F3
- .
-
- ================================== FAILURES ===================================
第一条用例执行失败,立即退出不在往下执行用例
- import pytest
- class TestClass():
- def test_zne(self):
- print(1)
- assert 1==2
-
- def test_two(self):
- print(2)
- assert 1==2
-
- def test_a(self):
- print(3)
- assert 1==1
-
- if __name__ == '__main__':
- pytest.main(['-x','-s'])
-
- ============================= test session starts =============================
- platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
- rootdir: D:\Users\72036454\Desktop\pythonProject\Base
- plugins: allure-pytest-2.9.45
- collected 3 items
-
- test_page.py 1
- F
-
- ================================== FAILURES ===================================
用例中,第二和第三条用例加上了装饰器,装饰器最后一个单词分别为“slow” 和 “faster” ,-m 拿着两个单词去识别带这个装饰器的用例,识别到就执行,没有识别到的就不执行。
-m后面接的是表达式:['-s','-m slow or faster'] 、['-s','-m slow and faster']、['-s','-m not slow'] 这些表达式都支持。
- import pytest
- class TestClass():
- def test_zne(self):
- print(1)
- assert 1==2
-
- @pytest.mark.slow
- def test_two(self):
- print(2)
- assert 1==2
-
- @pytest.mark.faster
- def test_a(self):
- print(3)
- assert 1==1
-
- if __name__ == '__main__':
- pytest.main(['-s','-m slow or faster'])
-
- ============================= test session starts =============================
- platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
- rootdir: D:\Users\72036454\Desktop\pythonProject\Base
- plugins: allure-pytest-2.9.45
- collected 3 items / 1 deselected / 2 selected
-
- test_page.py 2
- F3
- .
-
- ================================== FAILURES ===================================
通过表达式匹配用例的函数名去执行用例,not test_zne 意思是不执行“test_zne”这条用例,所以就会执行第二第三条。同理 ['-s','-k test_zne'] 表示只执行第一条。
- import pytest
- class TestClass():
- def test_zne(self):
- print(1)
- assert 1==2
-
- @pytest.mark.slow
- def test_two(self):
- print(2)
- assert 1==2
-
- @pytest.mark.faster
- def test_a(self):
- print(3)
- assert 1==1
-
- if __name__ == '__main__':
- pytest.main(['-s','-k not test_zne'])
-
- ============================= test session starts =============================
- platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
- rootdir: D:\Users\72036454\Desktop\pythonProject\Base
- plugins: allure-pytest-2.9.45
- collected 3 items / 1 deselected / 2 selected
-
- test_page.py 2
- F3
- .
-
- ================================== FAILURES ===================================
这才是重点,学会使用这个,剩余的都学会了
- import pytest
- class TestClass():
- def test_zne(self):
- print(1)
- assert 1==2
-
- @pytest.mark.slow
- def test_two(self):
- print(2)
- assert 1==2
-
- @pytest.mark.faster
- def test_a(self):
- print(3)
- assert 1==1
-
- if __name__ == '__main__':
- pytest.main(['-h'])

pytest-ordering实现自定义用例执行顺序pip install pytest-ordering
@pytest.mark.first@pytest.mark.second@pytest.mark.second_to_last@pytest.mark.last@pytest.mark.run('first')@pytest.mark.run('second')@pytest.mark.run('second_to_last')@pytest.mark.run('last')@pytest.mark.run(order=1)@pytest.mark.run(order=2)@pytest.mark.run(order=-2)@pytest.mark.run(order=-1)对于以上三张方法,经常使用的不多,第一个执行和最后一个执行比较常用。
最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走!

被百万人刷爆的软件测试题库!!!谁用谁知道!!!全网最全面试刷题小程序,手机就可以刷题,地铁上公交上,卷起来!
涵盖以下这些面试题板块:
1、软件测试基础理论 ,2、web,app,接口功能测试 ,3、网络 ,4、数据库 ,5、linux
6、web,app,接口自动化 ,7、性能测试 ,8、编程基础,9、hr面试题 ,10、开放性测试题,11、安全测试,12、计算机基础

资料获取方式 :
