├── .gitignore ├── README.md ├── conftest.py ├── test_class.py ├── test_fixtures.py ├── test_function.py ├── test_parametrize.py ├── test_skip.py ├── test_unittest.py ├── test_very_expensive_fixture.py └── tests.json /.gitignore: -------------------------------------------------------------------------------- 1 | .pytest_cache 2 | *.pyc 3 | .coverage 4 | htmlcov 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pytest-gallery 2 | 3 | A sampler of tests showing different ways to construct tests for pytest. I wanted a way to see how pytest handles all the variety without being distracted by actual product or test code. 4 | -------------------------------------------------------------------------------- /conftest.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | import pytest 4 | 5 | 6 | def pytest_collect_file(parent, path): 7 | if path.ext == ".json" and path.basename.startswith("test"): 8 | return JsonFile(path, parent) 9 | 10 | 11 | class JsonFile(pytest.File): 12 | def collect(self): 13 | with self.fspath.open() as jsonf: 14 | for word in json.load(jsonf): 15 | yield JsonItem(self, word) 16 | 17 | 18 | class JsonItem(pytest.Item): 19 | def __init__(self, parent, word): 20 | super().__init__(word, parent) 21 | self.word = word 22 | 23 | def runtest(self): 24 | assert self.word != "" 25 | -------------------------------------------------------------------------------- /test_class.py: -------------------------------------------------------------------------------- 1 | class TestBase: 2 | def setup_method(self): 3 | print("setup_method base") 4 | 5 | def teardown_method(self): 6 | print("teardown_method base") 7 | 8 | 9 | class TestClass(TestBase): 10 | 11 | def setup_class(self): 12 | print("setup_class") 13 | 14 | def teardown_class(self): 15 | print("teardown_class") 16 | 17 | def setup_method(self): 18 | super().setup_method() 19 | print("setup_method") 20 | 21 | def teardown_method(self): 22 | print("teardown_method") 23 | super().teardown_method() 24 | 25 | def test_method1(self): 26 | assert 1 == 1 27 | 28 | def test_method2(self): 29 | assert 2 == 2 30 | 31 | def test_method3(self): 32 | assert 3 == 3 33 | -------------------------------------------------------------------------------- /test_fixtures.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | @pytest.fixture 4 | def some_data(): 5 | return [1, 2, 3] 6 | 7 | 8 | def test_fixture(some_data): 9 | assert len(some_data) == 3 10 | 11 | 12 | @pytest.fixture 13 | def more_data(some_data): 14 | return [2*x for x in some_data] 15 | 16 | 17 | def test_two_fixtures(some_data, more_data): 18 | assert len(some_data) == len(more_data) 19 | 20 | 21 | @pytest.fixture(scope='session') 22 | def expensive_data(): 23 | return list(range(10)) 24 | 25 | 26 | def test_with_expensive_data(expensive_data): 27 | assert len(expensive_data) == 10 28 | 29 | 30 | def test_with_expensive_data2(expensive_data): 31 | assert len(expensive_data) == 10 32 | 33 | 34 | @pytest.fixture(params=[1, 2, 3]) 35 | def some_number(request): 36 | return request.param 37 | 38 | 39 | def test_parametrized_fixture(some_number): 40 | assert some_number > 0 41 | -------------------------------------------------------------------------------- /test_function.py: -------------------------------------------------------------------------------- 1 | def test_function1(): 2 | assert 1 == 1 3 | 4 | def test_function2(): 5 | assert 2 == 2 6 | -------------------------------------------------------------------------------- /test_parametrize.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | @pytest.mark.parametrize("x, ans", [ 4 | (1, 101), 5 | (2, 202), 6 | ]) 7 | def test_parametrized(x, ans): 8 | assert 100 * x + x == ans 9 | 10 | 11 | @pytest.mark.parametrize("x, ans", [ 12 | (1, 101), 13 | (2, 202), 14 | ], ids=['one', 'two']) 15 | def test_parametrized_with_id(x, ans): 16 | assert 100 * x + x == ans 17 | 18 | 19 | @pytest.mark.parametrize("x", [1, 2]) 20 | @pytest.mark.parametrize("y", [3, 4]) 21 | def test_parametrized_twice(x, y): 22 | assert x + y > 0 23 | -------------------------------------------------------------------------------- /test_skip.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | import pytest 4 | 5 | 6 | def should_skip(): 7 | return True 8 | 9 | 10 | @pytest.mark.skipif(sys.version_info < (2, 0), reason="Python 1.x") 11 | def test_always_run(): 12 | assert 1 == 1 13 | 14 | 15 | @pytest.mark.skipif(sys.version_info > (2, 0), reason="Python 2.x+") 16 | def test_never_run(): 17 | assert 1 == 13 18 | 19 | 20 | @pytest.mark.skipif(should_skip(), reason="should_skip") 21 | def test_always_skip(): 22 | assert 1 == 13 23 | 24 | 25 | @pytest.mark.skipif('should_skip()', reason="should_skip") 26 | def test_always_skip_string(): 27 | assert 1 == 13 28 | -------------------------------------------------------------------------------- /test_unittest.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | 3 | class MyTestCase(unittest.TestCase): 4 | items = [] 5 | 6 | @classmethod 7 | def setUpClass(cls): 8 | cls.items.append("hello") 9 | 10 | @classmethod 11 | def tearDownClass(cls): 12 | cls.items.pop() 13 | 14 | def setUp(self): 15 | self.number = 1 16 | 17 | def tearDown(self): 18 | self.number = None 19 | 20 | def test_method1(self): 21 | assert self.number == 1 22 | assert self.items[0] == "hello" 23 | 24 | def test_method2(self): 25 | assert self.number == 1 26 | assert self.items[0] == "hello" 27 | -------------------------------------------------------------------------------- /test_very_expensive_fixture.py: -------------------------------------------------------------------------------- 1 | import time 2 | 3 | import pytest 4 | 5 | configurations = [ 6 | pytest.param({"a": 1, "b": 99}, id="ab"), 7 | pytest.param({"a": 2}, id="a"), 8 | pytest.param({"a": 3, "b": 1723, "c": 1001}, id="abc"), 9 | ] 10 | 11 | @pytest.fixture(params=configurations, scope="session") 12 | def options(request): 13 | time.sleep(1) 14 | return request.param 15 | 16 | def test_with_options(options): 17 | assert "a" in options 18 | 19 | def test_with_options_2(options): 20 | assert "a" in options 21 | 22 | def test_with_options_3(options): 23 | assert "a" in options 24 | -------------------------------------------------------------------------------- /tests.json: -------------------------------------------------------------------------------- 1 | [ 2 | "hello", 3 | "goodbye" 4 | ] 5 | --------------------------------------------------------------------------------