Skip to content

Commit

Permalink
Recommend to use the name argument in the pytest.fixture decorator (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
weiiwang01 authored Apr 4, 2023
1 parent 8c5f2dc commit 7c41936
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- [Static Code Analysis](#static-code-analysis)
- [Subprocess calls within Python](#subprocess-calls-within-python)
- [Test Coverage](#test-coverage)
- [Test Fixture](#test-fixture)
- [Test Structure](#test-structure)
- [Type Hints](#type-hints)
- [When to use Python or Shell](#when-to-use-python-or-shell)
Expand Down Expand Up @@ -582,6 +583,29 @@ keeping in mind the option of breaking up the test into multiple tests.
This structure makes it easy to understand what is required before test
execution, how the test works and what it checks for in the end.
# Test Fixture
When declaring fixture functions and requesting them in the same Python module,
there is a risk of accidentally using the fixture function declared in the
global scope instead of requesting it in the test case function parameters.
To prevent this problem, it is recommended to add a "fixture" prefix or suffix
to the fixture function name and use the `name` argument in the `pytest.fixture`
decorator to name the fixture.

Here's an example of using the `name` argument in `pytest.fixture`:

```python
import pytest
@pytest.fixture(name="app")
def app_fixture():
return "app"
def test_my_fixture(app):
assert app == "app"
```

## Test Coverage

Unit tests check whether the intended functionality has been implemented. This
Expand Down

0 comments on commit 7c41936

Please sign in to comment.