Skip to content

Commit e329144

Browse files
Bernhard Kaindlbernhardkaindl
authored andcommitted
docs: tests/unit/conftest-README.md: Improve legibility, fix markdownlint
1 parent dc5af7a commit e329144

1 file changed

Lines changed: 48 additions & 35 deletions

File tree

tests/unit/conftest-README.md

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,36 @@
1-
# tests/unit/conftest-README:
1+
# Documentation of `tests/unit/conftest.py`
22

3-
This [README](../../README.md) documents the
4-
[pytest](https://docs.pytest.org/en/4.6.x/contents.html)
5-
[`conftest.py`](https://docs.pytest.org/en/4.6.x/pythonpath.html?highlight=conftest.py#test-modules-conftest-py-files-inside-packages)
6-
file [`tests/unit/conftest.py`](conftest.py).
3+
## Overview
4+
5+
`conftest.py` is a special file used by the `pytest` framework.
6+
It is a local plugin for your tests, where you can define fixtures, hooks,
7+
and other configuration for your tests.
8+
9+
`pytest` automatically discovers `conftest.py`, and makes
10+
the fixtures in it available to all tests in its directory.
11+
12+
This is described in the `pytest` documentation:
13+
<https://docs.pytest.org/en/stable/reference/fixtures.html#conftest-py-sharing-fixtures-across-multiple-files>
714

815
The tests in [`tests/unit/`](.) use it for three classes of tests:
16+
917
- [Unit-testing](https://en.wikipedia.org/wiki/Unit_testing) of single functions
1018
- [Functional testing](https://en.wikipedia.org/wiki/Functional_testing)
1119
of a chain of functions, testing their in- and output
1220
- [Integration testing](https://en.wikipedia.org/wiki/Integration_testing)
1321
of `xen-bugtool` the main code of [`xen-bugtool`](../../xen-bugtool)
1422
- by calling its function `main()` in [`tests/unit/test_main.py`](test_main.py)
1523

16-
It contains several [pytest](https://docs.pytest.org/en/4.6.x/contents.html)
17-
fixtures which are described below.
18-
19-
20-
## Overview
21-
22-
`conftest.py` is a special file used by the pytest framework.
23-
It is a local plugin for your tests, where you can define fixtures, hooks,
24-
and other configuration for your tests.
25-
26-
`pytest` automatically discovers `conftest.py`, and makes
27-
the fixtures in it available to all tests in its directory.
28-
29-
## Fixtures
24+
## Fixtures provided by `tests/unit/conftest.py`
3025

3126
Fixtures are functions that set up and tear down test environments.
3227
They are decorated with `@pytest.fixture`.
3328
Fixtures defined in `conftest.py` are available to all test files
3429
in the same directory and subdirectories.
3530

36-
Links to the [pytest](https://docs.pytest.org/en/8.0.x/how-to/index.html#how-to)
31+
Links to the [`pytest`](https://docs.pytest.org/en/8.0.x/how-to/index.html#how-to)
3732
documentation pages about fixtures:
33+
3834
- [An introduction to fixtures](https://docs.pytest.org/en/8.0.x/fixture.html):
3935
Quick intro into the topic of fixtures
4036
- [How to use fixtures](https://docs.pytest.org/en/8.0.x/how-to/fixtures.html):
@@ -44,6 +40,7 @@ documentation pages about fixtures:
4440

4541
These are the fixtures defined in [`tests/unit/conftest.py`](conftest.py),
4642
their name and purpose is:
43+
4744
- `builtins`: Provide the name of the built-in module for Python 2.x and Python 3.x
4845
- `testdir`: Provide the directory of the unit test for locating files relative to it.
4946
- `dom0_template`: Provide the directory of the dom0 template.
@@ -62,29 +59,35 @@ their name and purpose is:
6259
## Fixtures
6360

6461
### `builtins`
65-
- **Purpose**: Provide the `builtins` module for __Python 2.x__ *and*
66-
__Python 3.x__
62+
63+
- **Purpose**: Provide the `builtins` module for *Python 2.x* and
64+
*Python 3.x*
6765

6866
- **Example**:
67+
6968
```python
7069
def test_example(builtins, mocker):
7170
# `builtins` provides the builtins module for Python 2 and Python 3:
7271
mocker.patch(builtins + ".open", mocker.mock_open(read_data="data"))
7372
```
7473

7574
### `testdir`
75+
7676
- **Purpose**: Provide the directory of the main [tests/](..) directory
7777
- **Example**:
78+
7879
```python
7980
@pytest.fixture
8081
def dom0_template(testdir):
8182
# relative to testdir, provide the dom0-template directory
82-
return testdir + "/../integration/dom0-template
83-
```
83+
return testdir + "/../integration/dom0-template"
84+
```
8485

8586
### `dom0_template`
87+
8688
- **Purpose**: Provide the directory of the dom0 template directory
8789
- **Example**:
90+
8891
```python
8992
def test_example(bugtool, dom0_template):
9093
# Provide fixtures and test cases with access to example files.
@@ -94,11 +97,13 @@ their name and purpose is:
9497
```
9598

9699
### `imported_bugtool`
100+
97101
- **Purpose**: Import the `xen-bugtool` script as a module for
98102
executing unit tests on functions.
99-
- **Scope**: The entire pytest session, it only runs once.
103+
- **Scope**: The entire `pytest` session, it only runs once.
100104
- **Use case**: Only for other fixtures that prepare it for tests.
101105
- **Example use**:
106+
102107
```python
103108
@pytest.fixture(scope="function")
104109
def bugtool(imported_bugtool):
@@ -113,33 +118,38 @@ their name and purpose is:
113118
```
114119

115120
### `bugtool`
116-
- **Purpose**: Initialize the bugtool data dict for each test.
121+
122+
- **Purpose**: Initialize the data dictionary for each test.
117123
- **Example**:
124+
118125
```python
119126
def test_example(bugtool):
120127
# Test specific functionalities of the bugtool using initialized data
121128
assert bugtool.data == {}
122129
```
123130

124131
### `in_tmpdir`
132+
125133
- **Purposes**:
126-
- Provide a pytest
127-
[`tmpdir`](https://docs.pytest.org/en/6.2.x/tmpdir.html#the-tmpdir-fixture)
128-
fixture.
134+
135+
- Provide the [`tmpdir`](https://docs.pytest.org/en/6.2.x/tmpdir.html#the-tmpdir-fixture)
136+
fixture.
129137
- `cd` into it before yielding control switch back afterwards.
130138
- Offer the [`py.path.local`](https://py.readthedocs.io/en/latest/path.htm)
131139
object which provides
132140
[`os.path`](https://docs.python.org/3/library/os.path.html).
133141
- See the **Example 2** below on how to use it.
134142

135143
- **Examples**:
144+
136145
```python
137146
# Example 1: Test requests the fixture using a decorator:
138147
@pytest.usefixtures("in_tmpdir")
139148
def test_runs_in_tmpdir(bugtool):
140149
assert bugtool.function_creating_files_in_the_cwd()
141-
# restore & cleanup is taken care of by the fixures.
150+
# restore & cleanup is taken care of by the fixtures.
142151
```
152+
143153
```python
144154
# Example 2: Get fixture as argument of type py.path.local:
145155
def test_prepares_tmpdir_for_testing(in_tmpdir, bugtool):
@@ -150,10 +160,12 @@ their name and purpose is:
150160
```
151161

152162
### `bugtool_log`
163+
153164
- **Purpose**: Like `in_tmpdir` and provide the bugtool fixture.
154165

155166
It adds checking the file located at `bugtool.XEN_BUGTOOL_LOG` for output.
156167
- **Example**:
168+
157169
```python
158170
def test_not_generating_unexpected_logs(bugtool_log):
159171
# This fixture fails this test and shows "message"
@@ -162,20 +174,21 @@ their name and purpose is:
162174
```
163175

164176
### `isolated_bugtool`
165-
- **Purpose**: Like `bugtool_log` and make the working
166-
directory read-only.
177+
178+
- **Purpose**: Like `bugtool_log` and make the working directory read-only.
167179
- **Example**:
180+
168181
```python
169182
def test_cannot_write_to_its_current_directory(isolated_bugtool):
170183
# The code under test can't write its working directory:
171184
open("file", "w") # will raise an Exception
172185
```
173186

174-
175187
## Further Reading
176188

177-
Links to the [pytest](https://docs.pytest.org/en/8.0.x/how-to/index.html#how-to)
178-
documentation pages about fixtures:
189+
Links to the [documentation](https://docs.pytest.org/en/8.0.x/how-to/index.html#how-to)
190+
pages about fixtures:
191+
179192
- [An introduction to fixtures](https://docs.pytest.org/en/8.0.x/fixture.html):
180193
Quick intro into the topic of fixtures
181194
- [How to use fixtures](https://docs.pytest.org/en/8.0.x/how-to/fixtures.html):

0 commit comments

Comments
 (0)