-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try supporting yield functions for setup/teardown. (#1)
* Support yield functions for setup/teardown. Co-authored-by: Blake Naccarato <[email protected]>
- Loading branch information
1 parent
e05fcdd
commit 32f9907
Showing
8 changed files
with
213 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ branch = True | |
[paths] | ||
source = | ||
src | ||
.tox/*/site-packages | ||
.tox/*/lib/*/site-packages | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,4 +36,3 @@ def test_error_during_teardown(x): | |
""" | ||
... | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import pytest | ||
|
||
|
||
def foo(): | ||
... | ||
|
||
@pytest.mark.param_scope(foo) | ||
def test_one_params_to_marker(): | ||
""" | ||
This also blows up, with_args required. | ||
You gotta use `@pytest.mark.param_scope.with_args(foo)` | ||
""" | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import pytest | ||
|
||
|
||
def setup_and_teardown(): | ||
print('\nsetup') | ||
yield 42 | ||
print('\nteardown') | ||
|
||
|
||
@pytest.mark.param_scope(setup_and_teardown, None) | ||
@pytest.mark.parametrize('x', ['a', 'b', 'c']) | ||
def test_yield(x, param_scope): | ||
assert param_scope == 42 | ||
|
||
|
||
|
||
def separate_teardown(): | ||
print('separate teardown') | ||
|
||
|
||
@pytest.mark.param_scope(setup_and_teardown, separate_teardown) | ||
@pytest.mark.parametrize('x', ['a', 'b', 'c']) | ||
def test_two_teardowns(x, param_scope): | ||
""" | ||
For now, we'll allow this odd use model. | ||
Weird, but really, why not? | ||
""" | ||
assert param_scope == 42 | ||
|
||
|
||
@pytest.mark.param_scope.with_args(setup_and_teardown) | ||
@pytest.mark.parametrize('x', ['a', 'b', 'c']) | ||
def test_just_one_func(x, param_scope): | ||
""" | ||
It's not pretty, but if you want to just pass in one, | ||
you gotta use "with_args". | ||
See "Passing a callable to custom markers" in pytest docs | ||
- https://docs.pytest.org/en/stable/example/markers.html#passing-a-callable-to-custom-markers | ||
""" | ||
assert param_scope == 42 | ||
|
||
|
||
@pytest.mark.param_scope | ||
@pytest.mark.parametrize('x', ['a', 'b', 'c']) | ||
def test_no_param_scope_args(x): | ||
""" | ||
No point in this, but it doesn't blow up | ||
""" | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
|
||
def test_yield(pytester): | ||
pytester.copy_example("examples/test_yield.py") | ||
result = pytester.runpytest('test_yield.py::test_yield', '-v', '-s') | ||
result.assert_outcomes(passed=3) | ||
result.stdout.re_match_lines( | ||
[ | ||
".*test_yield.a.*", | ||
"setup", | ||
".*test_yield.b.*", | ||
".*test_yield.c.*", | ||
"teardown", | ||
] | ||
) | ||
|
||
def test_two_teardowns(pytester): | ||
pytester.copy_example("examples/test_yield.py") | ||
result = pytester.runpytest('test_yield.py::test_two_teardowns', '-v', '-s') | ||
result.assert_outcomes(passed=3) | ||
result.stdout.re_match_lines( | ||
[ | ||
".*test_two_teardowns.a.*", | ||
"setup", | ||
".*test_two_teardowns.b.*", | ||
".*test_two_teardowns.c.*", | ||
"teardown", | ||
"separate teardown", | ||
] | ||
) | ||
|
||
def test_one_param(pytester): | ||
pytester.copy_example("examples/test_yield.py") | ||
result = pytester.runpytest('test_yield.py::test_just_one_func', '-v', '-s') | ||
result.assert_outcomes(passed=3) | ||
result.stdout.re_match_lines( | ||
[ | ||
".*test_just_one_func.a.*", | ||
"setup", | ||
".*test_just_one_func.b.*", | ||
".*test_just_one_func.c.*", | ||
"teardown", | ||
] | ||
) | ||
|
||
|
||
def test_no_params(pytester): | ||
pytester.copy_example("examples/test_yield.py") | ||
result = pytester.runpytest('test_yield.py::test_no_param_scope_args', '-v', '-s') | ||
result.assert_outcomes(passed=3) | ||
result.stdout.re_match_lines( | ||
[ | ||
".*test_no_param_scope_args.a.*", | ||
".*test_no_param_scope_args.b.*", | ||
".*test_no_param_scope_args.c.*", | ||
] | ||
) |