Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommend to use the name argument in the pytest.fixture decorator #55

Merged
merged 5 commits into from
Apr 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- [Static Code Analysis](#static-code-analysis)
- [Subprocess calls within Python](#subprocess-calls-within-python)
- [Test Coverage](#test-coverage)
- [Test Fixture](#test-fixture)
weiiwang01 marked this conversation as resolved.
Show resolved Hide resolved
- [Test Structure](#test-structure)
- [Type Hints](#type-hints)
- [When to use Python or Shell](#when-to-use-python-or-shell)
Expand Down Expand Up @@ -552,6 +553,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
jdkandersson marked this conversation as resolved.
Show resolved Hide resolved

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.
weiiwang01 marked this conversation as resolved.
Show resolved Hide resolved

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