-q 只显示整体测试结果
简化测试整体结果。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 ===================================
-s 用于显示测试函数中print()函数输出
显示测试用例中 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 ===================================
-x 在第一个错误或失败的测试中立即退出
第一条用例执行失败,立即退出不在往下执行用例
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 ===================================
-m 只运行带有装饰器配置的测试用例
用例中,第二和第三条用例加上了装饰器,装饰器最后一个单词分别为“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 ===================================
-k 通过表达式运行指定的测试用例
通过表达式匹配用例的函数名去执行用例,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 ===================================
-h 帮助
这才是重点,学会使用这个,剩余的都学会了
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的执行顺序:
- 默认情况下,pytest的执行顺序是自上往下的。
- 可以通过第三方插件
pytest-ordering实现自定义用例执行顺序 - 官方文档: pytest-ordering.readthedocs.io/en/develop/
安装插件:
pip install pytest-ordering
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)
对于以上三张方法,经常使用的不多,第一个执行和最后一个执行比较常用。
学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!
一、Python所有方向的学习路线
Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
二、学习软件
工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。
三、全套PDF电子书
书籍的好处就在于权威和体系健全,刚开始学习的时候你可以只看视频或者听某个人讲课,但等你学完之后,你觉得你掌握了,这时候建议还是得去看一下书籍,看权威技术书籍也是每个程序员必经之路。
四、入门学习视频
我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。
五、实战案例
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
六、面试资料
我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。