1 测试脚本
- 在
pytest_study
文件夹下创立一个test_mm.py
# -*- coding:utf-8 -*-# 作者:NoamaNelson# 日期:2021/8/27 16:51# 文件名称:test_mm.py# 作用:xxx# 分割:VX(NoamaNelson)# 博客:https://blog.csdn.net/NoamaNelsondef m_sum(x): return x * (x+1)def test_m_sum(): assert m_sum(3) == 11
- 在
pytest_study
文件夹下关上cmd间接输出pytest运行:
(venv) F:\pytest_study>pytest========================================================================= test session starts ==========================================================================platform win32 -- Python 3.7.0, pytest-6.2.4, py-1.10.0, pluggy-0.13.1rootdir: F:\pytest_studyplugins: allure-pytest-2.8.12, cov-2.8.1, forked-1.1.3, html-2.0.1, metadata-1.8.0, ordering-0.6, xdist-1.31.0collected 1 itemtest_mm.py F [100%]=============================================================================== FAILURES ===============================================================================______________________________________________________________________________ test_m_sum ______________________________________________________________________________ def test_m_sum():> assert m_sum(3) == 11E assert 12 == 11E + where 12 = m_sum(3)test_mm.py:13: AssertionError======================================================================= short test summary info ========================================================================FAILED test_mm.py::test_m_sum - assert 12 == 11========================================================================== 1 failed in 0.07s ===========================================================================
- 发现执行失败了,是因为12不等于11,
assert
是用来进行断言的。
2 脚本剖析
2.1 断言应用assert
- 从上边脚本看到断言的话应用
assert
即可,依据官网的说法是pytest断言根本都是用的assert
;
2.2 应用pytest运行用例规定
test_*.py和*_test.py命名的函数
以test_结尾的函数
test_结尾的办法,不能有__init__ 办法
同python一样,包须要有__init__.py文件
- 以
-q
或-quiet
参数进行静默运行函数(说白了就是后果输入简单化)
3 练习下用例运行规定
- 先在
pytest_study
目录下再新建一个test_case
包;
- 而后把之前写的第一个用例
test_mm.py
挪动到这个目录下:
- 在pytest_study下执行pytest,发现是能够执行的,阐明是执行了
test_case
下的test_mm.py
(venv) F:\pytest_study>pytest========================================================================= test session starts ==========================================================================platform win32 -- Python 3.7.0, pytest-6.2.4, py-1.10.0, pluggy-0.13.1rootdir: F:\pytest_studyplugins: allure-pytest-2.8.12, cov-2.8.1, forked-1.1.3, html-2.0.1, metadata-1.8.0, ordering-0.6, xdist-1.31.0collected 1 itemtest_case\test_mm.py F [100%]=============================================================================== FAILURES ===============================================================================______________________________________________________________________________ test_m_sum ______________________________________________________________________________ def test_m_sum():> assert m_sum(3) == 11E assert 12 == 11E + where 12 = m_sum(3)test_case\test_mm.py:13: AssertionError======================================================================= short test summary info ========================================================================FAILED test_case/test_mm.py::test_m_sum - assert 12 == 11========================================================================== 1 failed in 0.13s ===========================================================================(venv) F:\pytest_study>
(venv) F:\pytest_study>pytest -qF [100%]=============================================================================== FAILURES ===============================================================================______________________________________________________________________________ test_m_sum ______________________________________________________________________________ def test_m_sum():> assert m_sum(3) == 11E assert 12 == 11E + where 12 = m_sum(3)test_case\test_mm.py:13: AssertionError======================================================================= short test summary info ========================================================================FAILED test_case/test_mm.py::test_m_sum - assert 12 == 111 failed in 0.05s(venv) F:\pytest_study>
- 再在
test_case
下新建一个名为test_a
的包,并在包里复制一个test_mm1.py
- 从执行后果看是ok的,那么以上都证实了文件名、包名都须要以test结尾能力被执行;
(venv) F:\pytest_study>pytest -qFF [100%]=============================================================================== FAILURES ===============================================================================______________________________________________________________________________ test_m_sum ______________________________________________________________________________ def test_m_sum():> assert m_sum(3) == 11E assert 12 == 11E + where 12 = m_sum(3)test_case\test_mm.py:13: AssertionError______________________________________________________________________________ test_m_sum ______________________________________________________________________________ def test_m_sum():> assert m_sum(3) == 11E assert 12 == 11E + where 12 = m_sum(3)test_case\test_a\test_mm1.py:13: AssertionError======================================================================= short test summary info ========================================================================FAILED test_case/test_mm.py::test_m_sum - assert 12 == 11FAILED test_case/test_a/test_mm1.py::test_m_sum - assert 12 == 112 failed in 0.20s(venv) F:\pytest_study>
- 咱们批改下
test_mm1.py
减少一个类,如下:
# -*- coding:utf-8 -*-# 作者:NoamaNelson# 日期:2021/8/27 16:51# 文件名称:test_mm.py# 作用:xxx# 分割:VX(NoamaNelson)# 博客:https://blog.csdn.net/NoamaNelsonimport pytestclass TestClass: def test_you(self): y = "you" assert "y" in y def test_hai(self): h = "hai" assert "gg" not in hif __name__ == '__main__': pytest.main()
- 间接在
pytest_study
下执行pytest -q
,如下,阐明类也执行到了:
(venv) F:\pytest_study>pytest -q... [100%]3 passed in 0.15s