-
-
Save oakkitten/975c05fd75c24cd55afdd5c565348523 to your computer and use it in GitHub Desktop.
cats & dogs
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
from contextlib import contextmanager | |
import pytest | |
dogs = set() | |
@contextmanager | |
def expensive_operation_with_dog_named(name): | |
print(f"expensive operation with dog named {name}") | |
dogs.add(name) | |
yield | |
dogs.remove(name) | |
cats = set() | |
@contextmanager | |
def expensive_operation_with_cat_named(name): | |
print(f"expensive operation with cat named {name}") | |
cats.add(name) | |
yield | |
cats.remove(name) | |
@pytest.fixture(scope="class") | |
def charlie(): | |
with expensive_operation_with_dog_named("charlie"): | |
yield | |
@pytest.fixture(scope="class") | |
def buddy(): | |
with expensive_operation_with_dog_named("buddy"): | |
yield | |
@pytest.fixture() | |
def tom(): | |
with expensive_operation_with_cat_named("tom"): | |
yield | |
@pytest.fixture() | |
def simba(): | |
with expensive_operation_with_cat_named("simba"): | |
yield | |
class TestWithCharlie: | |
def test_charlie_tom(self, charlie, tom): | |
assert dogs == {"charlie"} | |
assert cats == {"tom"} | |
def test_charlie_simba(self, charlie, simba): | |
assert dogs == {"charlie"} | |
assert cats == {"simba"} | |
class TestWithBuddy: | |
def test_buddy_tom(self, buddy, tom): | |
assert dogs == {"buddy"} | |
assert cats == {"tom"} | |
def test_buddy_simba(self, buddy, simba): | |
assert dogs == {"buddy"} | |
assert cats == {"simba"} | |
class TestWithSimbaBeforeBuddy: | |
@pytest.fixture(scope="function") | |
def buddy(self): | |
with expensive_operation_with_dog_named("buddy"): | |
yield | |
def test_simba_buddy(self, simba, buddy): | |
assert dogs == {"buddy"} | |
assert cats == {"simba"} | |
def test_tree(): | |
assert dogs == cats == set() | |
# example-optimized.py | |
# SETUP C charlie | |
# SETUP F tom | |
# example-optimized.py::TestWithCharlie::test_charlie_tom (fixtures used: charlie, tom) | |
# TEARDOWN F tom | |
# SETUP F simba | |
# example-optimized.py::TestWithCharlie::test_charlie_simba (fixtures used: charlie, simba) | |
# TEARDOWN F simba | |
# TEARDOWN C charlie | |
# SETUP C buddy | |
# SETUP F tom | |
# example-optimized.py::TestWithBuddy::test_buddy_tom (fixtures used: buddy, tom) | |
# TEARDOWN F tom | |
# SETUP F simba | |
# example-optimized.py::TestWithBuddy::test_buddy_simba (fixtures used: buddy, simba) | |
# TEARDOWN F simba | |
# TEARDOWN C buddy | |
# SETUP F simba | |
# SETUP F buddy | |
# example-optimized.py::TestWithSimbaBeforeBuddy::test_simba_buddy (fixtures used: buddy, simba) | |
# TEARDOWN F buddy | |
# TEARDOWN F simba | |
# example-optimized.py::test_tree |
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
from contextlib import contextmanager | |
import pytest | |
dogs = set() | |
@contextmanager | |
def expensive_operation_with_dog_named(name): | |
print(f"expensive operation with dog named {name}") | |
dogs.add(name) | |
yield | |
dogs.remove(name) | |
cats = set() | |
@contextmanager | |
def expensive_operation_with_cat_named(name): | |
print(f"expensive operation with cat named {name}") | |
cats.add(name) | |
yield | |
cats.remove(name) | |
@pytest.fixture() | |
def charlie(): | |
with expensive_operation_with_dog_named("charlie"): | |
yield | |
@pytest.fixture() | |
def buddy(): | |
with expensive_operation_with_dog_named("buddy"): | |
yield | |
@pytest.fixture() | |
def tom(): | |
with expensive_operation_with_cat_named("tom"): | |
yield | |
@pytest.fixture() | |
def simba(): | |
with expensive_operation_with_cat_named("simba"): | |
yield | |
def test_charlie_tom(charlie, tom): | |
assert dogs == {"charlie"} | |
assert cats == {"tom"} | |
def test_charlie_simba(charlie, simba): | |
assert dogs == {"charlie"} | |
assert cats == {"simba"} | |
def test_buddy_tom(buddy, tom): | |
assert dogs == {"buddy"} | |
assert cats == {"tom"} | |
def test_buddy_simba(buddy, simba): | |
assert dogs == {"buddy"} | |
assert cats == {"simba"} | |
def test_simba_buddy(simba, buddy): | |
assert dogs == {"buddy"} | |
assert cats == {"simba"} | |
def test_tree(): | |
assert dogs == cats == set() | |
# example.py | |
# SETUP F charlie | |
# SETUP F tom | |
# example.py::test_charlie_tom (fixtures used: charlie, tom) | |
# TEARDOWN F tom | |
# TEARDOWN F charlie | |
# SETUP F charlie | |
# SETUP F simba | |
# example.py::test_charlie_simba (fixtures used: charlie, simba) | |
# TEARDOWN F simba | |
# TEARDOWN F charlie | |
# SETUP F buddy | |
# SETUP F tom | |
# example.py::test_buddy_tom (fixtures used: buddy, tom) | |
# TEARDOWN F tom | |
# TEARDOWN F buddy | |
# SETUP F buddy | |
# SETUP F simba | |
# example.py::test_buddy_simba (fixtures used: buddy, simba) | |
# TEARDOWN F simba | |
# TEARDOWN F buddy | |
# SETUP F simba | |
# SETUP F buddy | |
# example.py::test_simba_buddy (fixtures used: buddy, simba) | |
# TEARDOWN F buddy | |
# TEARDOWN F simba | |
# example.py::test_tree |
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
import pytest | |
@pytest.fixture(scope="module") | |
def cat(): | |
yield "cat" | |
def test_cat_0(cat): | |
assert 1 | |
def test_cat_1(): | |
assert 1 | |
# $ python -m pytest module-level-bad.py --setup-plan | |
# module-level-bad.py | |
# SETUP M cat | |
# module-level-bad.py::test_cat_0 (fixtures used: cat) | |
# module-level-bad.py::test_cat_1 | |
# TEARDOWN M cat | |
# $ python -m pytest module-level-bad.py -k 1 --setup-plan | |
# module-level-bad.py | |
# module-level-bad.py::test_cat_1 |
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
from contextlib import contextmanager | |
import pytest | |
dogs = set() | |
@contextmanager | |
def expensive_operation_with_dog_named(name): | |
print(f"expensive operation with dog named {name}") | |
dogs.add(name) | |
yield | |
dogs.remove(name) | |
cats = set() | |
@contextmanager | |
def expensive_operation_with_cat_named(name): | |
print(f"expensive operation with cat named {name}") | |
cats.add(name) | |
yield | |
cats.remove(name) | |
@pytest.fixture(scope="only the tests that use this fixture") | |
def charlie(): | |
with expensive_operation_with_dog_named("charlie"): | |
yield | |
@pytest.fixture(scope="only the tests that use this fixture") | |
def buddy(): | |
with expensive_operation_with_dog_named("buddy"): | |
yield | |
@pytest.fixture(scope="only the tests that use this fixture") | |
def tom(): | |
with expensive_operation_with_cat_named("tom"): | |
yield | |
@pytest.fixture(scope="only the tests that use this fixture") | |
def simba(): | |
with expensive_operation_with_cat_named("simba"): | |
yield | |
def test_charlie_tom(charlie, tom): | |
assert dogs == {"charlie"} | |
assert cats == {"tom"} | |
def test_charlie_simba(charlie, simba): | |
assert dogs == {"charlie"} | |
assert cats == {"simba"} | |
def test_buddy_tom(buddy, tom): | |
assert dogs == {"buddy"} | |
assert cats == {"tom"} | |
def test_buddy_simba(buddy, simba): | |
assert dogs == {"buddy"} | |
assert cats == {"simba"} | |
def test_simba_buddy(simba, buddy): | |
assert dogs == {"buddy"} | |
assert cats == {"simba"} | |
def test_tree(): | |
assert dogs == cats == set() | |
# setup plan | |
# setup charlie | |
# setup tom | |
# test_charlie_tom | |
# teardown tom | |
# setup simba | |
# test_charlie_simba | |
# teardown simba | |
# teardown charlie | |
# setup buddy | |
# setup tom | |
# test_buddy_tom | |
# teardown tom | |
# setup simba | |
# test_buddy_simba | |
# teardown simba | |
# teardown buddy | |
# setup simba | |
# setup buddy | |
# test_simba_buddy | |
# teardown buddy | |
# teardown simba | |
# test_tree |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment