Installation and Getting Started
Support Python version : Python 2.6,2.7,3.3,3.4,3.5, Jython, PyPy- 2.3
Supported platforms : Unix / Posix and Windows
PyPI package name : pytest
Dependency : py, colorama (Windows)
PDF document : Download the latest version of a document
Pytest the creation is a simple and scalable test case has become very convenient framework. Test case is clear, easy to read without a lot of complicated code. Just a few minutes you can expand a small unit test your application or library or complex functional tests.
Installation Pytest
Execute the following command at the command line
pip install -U pytest
Check that the installed version Pytest
$ pytest --version
This is pytest version 3.x.y,imported from $PYTHON_PREFIX/lib/python3.6/site-packages/pytest.py
Creating your first test case
Only four lines of code to create a simple test:
# test_sample.py文件内容
def func(x):
return x + 1
def test_answer():
assert func(3) == 5
It's that simple. Now you can perform about this test case:
$ pytest
=========================== test session starts ============================
platform linux -- Python 3.x.y,pytest-3.x.y,py-1.x.y,pluggy-0.x.y
rootdir: $REGENDOC_TMPDIR,inifile:
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:5: AssertionError
========================= 1 failed in 0.12 seconds =========================
Because func(3)
it does not mean 5
that this test result information returned a failure.
Note:
You can use theassert
statement to assert expected results of your test cases. Senior assertion introspection Pytest can intelligently display intermediate results assertion expression, to avoid the variable name comes from the method in JUnit repeat the question.
The implementation of a number of test cases
pytest
Command execution of the current directory and all subdirectories test_*.py
and *_test.py
file formats. In general, use cases need to follow standard test discovery rules.
Assertion throws an exception specified
Use raise
exception may be thrown in the corresponding code specified:
# test_sysexit.py文件内容
import pytest
def f():
raise SystemExit(1)
def test_mytest():
with pytest.raises(SystemExit):
f()
Use "silent" mode, perform this test, such as:
$ pytest -q test_sysexit.py
. [100%]
1 passed in 0.12 seconds
Class organizations use multiple test cases
Once you need to develop a number of test cases, you might want to use the class to organize them. Use Pytest can easily create a test class that contains multiple use cases:
# test_class.py文件内容
class TestClass(object):
def test_one(self):
x = "this"
assert 'h' in x
def test_two(self):
x = "hello"
assert hasattr(x,'check')
Pytest
Python can be found in all test cases found in use cases follow the agreed rules, so it can be found Test
outside at the beginning of the test class to class and all test_
functions and methods beginning. Test category no longer inherit any object. We simply need to run this module to the file name.
$ 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
1 failed,1 passed in 0.12 seconds
The first use case is executed successfully, the next day failed to execute use cases. You can easily understand the cause of the failure to intermediate values of assertions by variables.
Function test request to use a separate temporary directory
Pytest
It provides built-in fixtures method parameters to use any resource, such as a separate temporary directory:
# test_tmpdir.py文件内容
def test_needsfiles(tmpdir):
print (tmpdir)
assert 0
Use the test function tmpdir
as a parameter, Pytest to find and call the fixture factory method to create the appropriate resources before the test case function call. Prior to the test run, Pytest create a separate temporary directory for each test case:
$ 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
1 failed in 0.12 seconds
For more information about tmpdir process, see: temporary directories and files
Further reading
See other documents pytest resources to help you build custom test cases and unique workflow:
- "Using pytest -m pytest to call pyest" - calling example command line
- "Using pytest with the original test suite" - a test case before use
- "Using tag attributes test case" -
pytest.mark
Related Information - "Pytest fixtures: explicit, modular, scalable" - function provides a reference for your test
- "Plug-write" - write plug-ins and Management
- "Quality Integration Practice" - virtual environments and test stratified