Skip to content

Commit

Permalink
Add tests and update README
Browse files Browse the repository at this point in the history
  • Loading branch information
will-byrne-cardano committed Oct 21, 2024
1 parent 98773f3 commit 49986d1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,41 @@ Running the tests is required to accurately record which fixtures are used, as p
$ pip install pytest-unused-fixtures
```

## Options

| Option Name | Type | Description |
|---------------------------------------|------------------|----------------------------------------------------------------|
| `--unused-fixtures` | switch | Enable plugin |
| `--unused-fixtures-ignore-path` | string* | Ignore paths for consideration of unused fixtures |
| `--unused-fixtures-context` | array\<string\> | Only consider fixtures missing if defined in these directories |
| `--unused-fixtures-fail-when-present` | switch | Fail pytest session if unused fixtures are present |


## Usage

After installing the package, the plugin is enabled by adding the switch `--unused-fixtures`.

Paths of fixtures can be ignored with one or multiple `--unused-fixtures-ignore-path` arguments. For example `--unused-fixtures-ignore-path=venv` will ignore all fixtures defined in the `venv` folder.

Alternatively, you can limit the scope in which the plugin looks for unused fixtures to a specific directory or directories. For example:

**Limit scope to one directory**
This example will only display unused fixtures that were defined in the `tests` folder
```shell
pytest tests --unused-fixtures --unused-fixtures-context tests
```

**Limit scope to multiple directories**
This example will only display unused fixtures that were defined in the `directory1` and `directory2/sub-directory` folders
```shell
pytest tests --unused-fixtures --unused-fixtures-context directory1 directory2/sub-directory
```

**Fail test session**
By default, when you use the `--unused-fixtures` switch, the plugin will exit with the same exit code pytest
would have used if running without the plugin. Add the switch `--unused-fixtures-fail-when-present` and the
pytest session will return a non-zero exit code if there are unused fixtures.

### Ignoring specific fixtures from report

Sometimes there will be fixture which are unused on purpose, for example when used in tests which are skipped by default. A decorator is provided for ignoring fixtures from the unused report. See the example for usage:
Expand Down
22 changes: 22 additions & 0 deletions tests/test_base_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,25 @@ def test_default(pytester, sample_testfile):
],
consecutive=True,
)


def test_option_context(pytester, sample_testfile):
"""
test the option `--unused-fixtures-context`.
"""
result = pytester.runpytest("--unused-fixtures", "--unused-fixtures-context", Path(__file__).parent)
result.assert_outcomes(passed=2)
result.stdout.no_fnmatch_line("*UNUSED FIXTURES*")


def test_fail_when_present(pytester, sample_testfile):
result = pytester.runpytest("--unused-fixtures", "--unused-fixtures-fail-when-present")
result.stdout.fnmatch_lines(["*ERROR: Unused fixtures failure: total of 1 unused fixtures*"])
result.stdout.fnmatch_lines(
[
"*UNUSED FIXTURES*",
"*fixtures defined from test_fail_when_present*",
"*fixture_c -- test_fail_when_present.py:12*",
],
)
assert result.ret == pytest.ExitCode.TESTS_FAILED

0 comments on commit 49986d1

Please sign in to comment.