Skip to content

Commit 0102e5b

Browse files
committed
Add invocation-scoped fixtures and small fixes
1 parent 5883c9c commit 0102e5b

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

content/posts/2016/whats-new-in-pytest-30.md

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,19 @@ Summary: Summary of new the features in pytest 3.0, compared to 2.9.
77

88
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.
99

10-
# New entry point: ``pytest``
10+
# Additional command alias: `pytest`
1111

1212
pytest came into existence as part of the [py](https://readthedocs.org/projects/pylib/) library, providing a tool called `py.test`.
1313

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-
1614
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.
1715

1816
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.
1917

2018
Note that `py.test` still works.
2119

22-
# ``approx()``: function for comparing floating-point numbers
20+
# `approx()`: function for comparing floating-point numbers
2321

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:
2523

2624
:::python
2725
from pytest import approx
@@ -44,7 +42,7 @@ Fixtures marked with `@pytest.fixture` can now use `yield` statements to provide
4442
yield smtp
4543
smtp.close()
4644

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:
4846

4947
@pytest.fixture(scope="module")
5048
def smtp(request):
@@ -71,8 +69,9 @@ auto-use fixtures to provide names to doctests.
7169
def add_np(doctest_namespace):
7270
doctest_namespace['np'] = numpy
7371

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:
7573

74+
:::python
7675
# content of numpy.py
7776
def arange():
7877
"""
@@ -110,7 +109,7 @@ This solves the problem where the function argument shadows the argument name, w
110109
# `pytest.raises`: regular expressions and custom messages
111110

112111
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`:
114113

115114
:::python
116115
import pytest
@@ -137,6 +136,30 @@ Also, the context manager form accepts a `message` keyword parameter to raise a
137136
with pytest.raises(KeyError, message="Key error not raised for {}".format(val)):
138137
check_input(val)
139138

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+
140163
# New hooks
141164

142165
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:
182205
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.
183206

184207
* `--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`.
186209

187210
# Documentation Restructuring
188211

@@ -207,7 +230,7 @@ The pytest team took the opportunity to discuss how to handle feature deprecatio
207230

208231
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.
209232

210-
## Deadline for deprecated features: new major version.
233+
## Deadline for deprecated features: new major version
211234

212235
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.
213236

@@ -218,5 +241,10 @@ The following features have been considered officially deprecated in pytest-3.0,
218241
* `yield-based` tests; use `@pytest.mark.parametrize` instead;
219242
* `pytest_funcarg__` prefix to declare fixtures; use `@pytest.fixture` decorator instead;
220243

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.
247+
248+
221249

222250

0 commit comments

Comments
 (0)