You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/posts/2016/whats-new-in-pytest-30.md
+38-10Lines changed: 38 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,21 +7,19 @@ Summary: Summary of new the features in pytest 3.0, compared to 2.9.
7
7
8
8
This blog post provides a short overview of some of the major features and changes in pytest 3.0. See the [CHANGELOG](http://doc.pytest.org/en/latest/changelog.html) for the complete list.
9
9
10
-
# New entry point: ``pytest``
10
+
# Additional command alias: `pytest`
11
11
12
12
pytest came into existence as part of the [py](https://readthedocs.org/projects/pylib/) library, providing a tool called `py.test`.
13
13
14
-
When pytest as a test runner came into existence, it was actually a tool in the [py](https://readthedocs.org/projects/pylib/) library called `py.test`.
15
-
16
14
Even after pytest was moved to a separate project, the `py.test` name for the command-line tool was kept to preserve backward compatibility with existing scripts and tools.
17
15
18
16
In pytest-3.0 the command-line tool is now called `pytest`, which is easier to type and prevents some common misunderstandings when tools with the same name are installed in the system.
19
17
20
18
Note that `py.test` still works.
21
19
22
-
# ``approx()``: function for comparing floating-point numbers
20
+
# `approx()`: function for comparing floating-point numbers
23
21
24
-
The ``approx`` function makes it easy to perform floating-point comparisons using a syntax that's as intuitive and close to pytest's philosophy:
22
+
The `approx` function makes it easy to perform floating-point comparisons using a syntax that's as intuitive and close to pytest's philosophy:
25
23
26
24
:::python
27
25
from pytest import approx
@@ -44,7 +42,7 @@ Fixtures marked with `@pytest.fixture` can now use `yield` statements to provide
44
42
yield smtp
45
43
smtp.close()
46
44
47
-
This makes it easier to change an existing fixture that uses `return` to return its values to use `yield` syntax if teardown code is needed later. Also, many users consider this style more clearer and natural than the previous method of registering a finalizer function using `request.addfinalizer` because the flow of the test is more explicit. Consider:
45
+
This makes it easier to change an existing fixture that uses `return` to use `yield` syntax if teardown code is needed later. Also, many users consider this style more clearer and natural than the previous method of registering a finalizer function using `request.addfinalizer` because the flow of the test is more explicit. Consider:
48
46
49
47
@pytest.fixture(scope="module")
50
48
def smtp(request):
@@ -71,8 +69,9 @@ auto-use fixtures to provide names to doctests.
71
69
def add_np(doctest_namespace):
72
70
doctest_namespace['np'] = numpy
73
71
74
-
Doctests in the same below the `conftest.py` file can now use the `numpy` module directly:
72
+
Doctests below the `conftest.py` file in the directory hierarchy can now use the `numpy` module directly:
75
73
74
+
:::python
76
75
# content of numpy.py
77
76
def arange():
78
77
"""
@@ -110,7 +109,7 @@ This solves the problem where the function argument shadows the argument name, w
110
109
# `pytest.raises`: regular expressions and custom messages
111
110
112
111
The `ExceptionInfo.match` method can be used to check exception messages using regular expressions, similar to `TestCase.assertRaisesRegexp` method
113
-
from ``unittest``:
112
+
from `unittest`:
114
113
115
114
:::python
116
115
import pytest
@@ -137,6 +136,30 @@ Also, the context manager form accepts a `message` keyword parameter to raise a
137
136
with pytest.raises(KeyError, message="Key error not raised for {}".format(val)):
138
137
check_input(val)
139
138
139
+
# `invocation`-scoped fixtures
140
+
141
+
An `invocation`-scoped fixture is cached in the same way as the fixture or test function that requests it.
142
+
143
+
For example:
144
+
145
+
::python
146
+
@pytest.fixture(scope='invocation')
147
+
def process_manager():
148
+
"""
149
+
Return a ProcessManager instance which can be used to start
150
+
long-lived processes and ensures they are terminated at the
151
+
appropriate scope.
152
+
"""
153
+
m = ProcessManager()
154
+
yield m
155
+
m.shutdown_all()
156
+
157
+
The `process_manager` fixture can be requested from test functions or fixtures of any scope, with each scope having its own `ProcessManager` instance.
158
+
159
+
Also, `monkeypatch` is now `invocation`-scoped it so can be used from `session`-scoped fixtures where previously you would get an error that you can not use a `function`-scoped fixture from a `session`-scoped one.
160
+
161
+
See the [docs](http://doc.pytest.org/en/features/invocation-fixture.html) for more information.
162
+
140
163
# New hooks
141
164
142
165
pytest-3.0 adds new hooks, useful both for plugin authors and local `conftest.py` plugins:
@@ -182,7 +205,7 @@ This will result in the following tests:
182
205
Overrides values from the configuration file (`pytest.ini`). For example, `"-o xfail_strict=True"` will make all `xfail` markers act as `strict`, failing the test suite if they exit with `XPASS` status.
183
206
184
207
*`--pdbcls`:
185
-
Allow passing a custom debugger class using `<module>:<class:` syntax, for example `--pdbcls=IPython.core.debugger:Pdb`.
208
+
Allow passing a custom debugger class that should be used together with the `--pdb` option. Syntax is in the form `<module>:<class:`, for example `--pdbcls=IPython.core.debugger:Pdb`.
186
209
187
210
# Documentation Restructuring
188
211
@@ -207,7 +230,7 @@ The pytest team took the opportunity to discuss how to handle feature deprecatio
207
230
208
231
Not showing them by default is confusing to users, as most people don't know how to display them in the first place. Also, it will help wider adoption of new ways of using pytest's features.
209
232
210
-
## Deadline for deprecated features: new major version.
233
+
## Deadline for deprecated features: new major version
211
234
212
235
Following [semantic versioning](http://semver.org/), the pytest team will add deprecation warnings to features which are considered obsolete because there are better alternatives or usage is low while maintenance burden on the team is high. Deprecated features will only be removed on the next **major** release, for example pytest-4.0, giving users and plugin authors ample time to update.
213
236
@@ -218,5 +241,10 @@ The following features have been considered officially deprecated in pytest-3.0,
218
241
*`yield-based` tests; use `@pytest.mark.parametrize` instead;
219
242
*`pytest_funcarg__` prefix to declare fixtures; use `@pytest.fixture` decorator instead;
220
243
244
+
# Small improvements and bug-fixes
245
+
246
+
As usual, this release includes a lot of small improvements and bug fixes. Make sure to checkout the full [CHANGELOG](http://doc.pytest.org/en/latest/changelog.html) for the complete list.
0 commit comments