** Python ** :Python 3.6,3.7,3.8,3.9,PyPy3 平台 :Linux和Windows PYPI包名称 : pytest PDF文档 : download latest
创建第一个测试¶用四行代码创建一个简单的测试函数: # content of test_sample.py def func(x): return x + 1 def test_answer(): assert func(3) == 5 就是这样。现在可以执行测试功能: $ pytest =========================== test session starts ============================ platform linux -- Python 3.x.y, pytest-6.x.y, py-1.x.y, pluggy-0.x.y cachedir: $PYTHON_PREFIX/.pytest_cache rootdir: $REGENDOC_TMPDIR collected 1 item test_sample.py F [100%] ================================= FAILURES ================================= _______________________________ test_answer ________________________________ def test_answer(): > assert func(3) == 5 E assert 4 == 5 E + where 4 = func(3) test_sample.py:6: AssertionError ========================= short test summary info ========================== FAILED test_sample.py::test_answer - assert 4 == 5 ============================ 1 failed in 0.12s ============================= 这个 注解 你可以使用 运行多个测试¶
断言引发了某个异常¶使用 raises 助手来断言某些代码引发异常: # content of test_sysexit.py import pytest def f(): raise SystemExit(1) def test_mytest(): with pytest.raises(SystemExit): f() 以“安静”报告模式执行测试功能: $ pytest -q test_sysexit.py . [100%] 1 passed in 0.12s 注解 这个 将一个类中的多个测试分组¶一旦开发了多个测试,您可能需要将它们分组到一个类中。pytest使创建包含多个测试的类变得很容易: # content of test_class.py class TestClass: def test_one(self): x = "this" assert "h" in x def test_two(self): x = "hello" assert hasattr(x, "check")
$ pytest -q test_class.py .F [100%] ================================= FAILURES ================================= ____________________________ TestClass.test_two ____________________________ self = <test_class.TestClass object at 0xdeadbeef> def test_two(self): x = "hello" > assert hasattr(x, "check") E AssertionError: assert False E + where False = hasattr('hello', 'check') test_class.py:8: AssertionError ========================= short test summary info ========================== FAILED test_class.py::TestClass::test_two - AssertionError: assert False 1 failed, 1 passed in 0.12s 第一次测试通过,第二次失败。您可以很容易地看到断言中的中间值,以帮助您理解失败的原因。 将测试分组在类中是有益的,原因如下:
在类中对测试分组时需要注意的是,每个测试都有一个唯一的类实例。让每个测试共享同一个类实例将非常不利于测试隔离,并且会导致不良的测试实践。概述如下: # content of test_class_demo.py class TestClassDemoInstance: def test_one(self): assert 0 def test_two(self): assert 0 $ pytest -k TestClassDemoInstance -q FF [100%] ================================= FAILURES ================================= ______________________ TestClassDemoInstance.test_one ______________________ self = <test_class_demo.TestClassDemoInstance object at 0xdeadbeef> def test_one(self): > assert 0 E assert 0 test_class_demo.py:3: AssertionError ______________________ TestClassDemoInstance.test_two ______________________ self = <test_class_demo.TestClassDemoInstance object at 0xdeadbeef> def test_two(self): > assert 0 E assert 0 test_class_demo.py:6: AssertionError ========================= short test summary info ========================== FAILED test_class_demo.py::TestClassDemoInstance::test_one - assert 0 FAILED test_class_demo.py::TestClassDemoInstance::test_two - assert 0 2 failed in 0.12s 请求功能测试的唯一临时目录¶
# content of test_tmpdir.py def test_needsfiles(tmpdir): print(tmpdir) assert 0 列出名字 $ pytest -q test_tmpdir.py F [100%] ================================= FAILURES ================================= _____________________________ test_needsfiles ______________________________ tmpdir = local('PYTEST_TMPDIR/test_needsfiles0') def test_needsfiles(tmpdir): print(tmpdir) > assert 0 E assert 0 test_tmpdir.py:3: AssertionError --------------------------- Captured stdout call --------------------------- PYTEST_TMPDIR/test_needsfiles0 ========================= short test summary info ========================== FAILED test_tmpdir.py::test_needsfiles - assert 0 1 failed in 0.12s 有关TMPDIR处理的更多信息,请访问 Temporary directories and files . 找出什么样的内置 pytest fixtures 使用以下命令存在: pytest --fixtures # shows builtin and custom fixtures 注意,这个命令省略了前导的fixtures 继续阅读¶查看其他Pytest资源以帮助您为独特的工作流自定义测试:
|
|
来自: wenxuefeng360 > 《待分类1》