|
1 | 1 | # -*- coding: utf-8 -*-
|
2 | 2 | import os
|
| 3 | +import re |
| 4 | +import shutil |
3 | 5 |
|
4 | 6 | import pytest
|
5 | 7 |
|
6 | 8 | import pytest_order
|
7 | 9 |
|
8 | 10 |
|
9 |
| -def fixture_path(): |
10 |
| - base_path = os.path.split(os.path.dirname(__file__))[0] |
11 |
| - return os.path.join(base_path, "fixture") |
| 11 | +@pytest.fixture(scope="module") |
| 12 | +def fixture_path(tmpdir_factory): |
| 13 | + fixture_path = str(tmpdir_factory.mktemp("fixtures")) |
| 14 | + testname = os.path.join(fixture_path, "test_classes.py") |
| 15 | + test_class_contents = """ |
| 16 | +import pytest |
| 17 | +
|
| 18 | +class Test1: |
| 19 | + @pytest.mark.order("last") |
| 20 | + def test_two(self): |
| 21 | + assert True |
| 22 | +
|
| 23 | + @pytest.mark.order("first") |
| 24 | + def test_one(self): |
| 25 | + assert True |
| 26 | +
|
| 27 | +class Test2: |
| 28 | + @pytest.mark.order("last") |
| 29 | + def test_two(self): |
| 30 | + assert True |
| 31 | +
|
| 32 | + @pytest.mark.order("first") |
| 33 | + def test_one(self): |
| 34 | + assert True |
| 35 | +
|
| 36 | +@pytest.mark.order("last") |
| 37 | +def test_two(): |
| 38 | + assert True |
| 39 | +
|
| 40 | +@pytest.mark.order("first") |
| 41 | +def test_one(): |
| 42 | + assert True |
| 43 | +""" |
| 44 | + with open(testname, "w") as fi: |
| 45 | + fi.write(test_class_contents) |
| 46 | + |
| 47 | + test_function_contents = """ |
| 48 | +import pytest |
| 49 | +
|
| 50 | +@pytest.mark.order("last") |
| 51 | +def test1_two(): |
| 52 | + assert True |
| 53 | +
|
| 54 | +@pytest.mark.order("first") |
| 55 | +def test1_one(): |
| 56 | + assert True |
| 57 | + """ |
| 58 | + testname = os.path.join(fixture_path, "test_functions1.py") |
| 59 | + with open(testname, "w") as fi: |
| 60 | + fi.write(test_function_contents) |
| 61 | + test_function_contents = """ |
| 62 | +import pytest |
12 | 63 |
|
| 64 | +@pytest.mark.order("last") |
| 65 | +def test2_two(): |
| 66 | + assert True |
13 | 67 |
|
14 |
| -def test_session_scope(capsys): |
15 |
| - args = ["-v", fixture_path()] |
| 68 | +@pytest.mark.order("first") |
| 69 | +def test2_one(): |
| 70 | + assert True |
| 71 | + """ |
| 72 | + testname = os.path.join(fixture_path, "test_functions2.py") |
| 73 | + with open(testname, "w") as fi: |
| 74 | + fi.write(test_function_contents) |
| 75 | + yield fixture_path |
| 76 | + shutil.rmtree(fixture_path, ignore_errors=True) |
| 77 | + |
| 78 | + |
| 79 | +@pytest.fixture(scope="module") |
| 80 | +def fixture_file_paths(fixture_path): |
| 81 | + yield [ |
| 82 | + os.path.join(fixture_path, "test_classes.py"), |
| 83 | + os.path.join(fixture_path, "test_functions1.py"), |
| 84 | + os.path.join(fixture_path, "test_functions2.py") |
| 85 | + ] |
| 86 | + |
| 87 | + |
| 88 | +def test_session_scope(fixture_path, capsys): |
| 89 | + args = ["-v", fixture_path] |
| 90 | + pytest.main(args, [pytest_order]) |
| 91 | + out, err = capsys.readouterr() |
| 92 | + expected = ( |
| 93 | + ".*test_classes.py::Test1::test_one" |
| 94 | + ".*test_classes.py::Test2::test_one" |
| 95 | + ".*test_classes.py::test_one" |
| 96 | + ".*test_functions1.py::test1_one" |
| 97 | + ".*test_functions2.py::test2_one" |
| 98 | + ".*test_classes.py::Test1::test_two" |
| 99 | + ".*test_classes.py::Test2::test_two" |
| 100 | + ".*test_classes.py::test_two" |
| 101 | + ".*test_functions1.py::test1_two" |
| 102 | + ".*test_functions2.py::test2_two.*" |
| 103 | + ) |
| 104 | + assert re.match(expected, out.replace("\n", "")), out |
| 105 | + |
| 106 | + |
| 107 | +def test_session_scope_multiple_files(fixture_file_paths, capsys): |
| 108 | + """Test that session scope works with multiple files on command line.""" |
| 109 | + args = ["-v"] + fixture_file_paths |
16 | 110 | pytest.main(args, [pytest_order])
|
17 | 111 | out, err = capsys.readouterr()
|
18 |
| - i_class1_one = out.index("test_classes.py::Test1::test_one") |
19 |
| - i_class2_one = out.index("test_classes.py::Test2::test_one") |
20 |
| - i_noclass_one = out.index("test_classes.py::test_one") |
21 |
| - i_func1_one = out.index("test_functions1.py::test_one") |
22 |
| - i_func2_one = out.index("test_functions2.py::test_one") |
23 |
| - i_class1_two = out.index("test_classes.py::Test1::test_two") |
24 |
| - i_class2_two = out.index("test_classes.py::Test2::test_two") |
25 |
| - i_noclass_two = out.index("test_classes.py::test_two") |
26 |
| - i_func1_two = out.index("test_functions1.py::test_two") |
27 |
| - i_func2_two = out.index("test_functions2.py::test_two") |
28 |
| - assert (i_class1_one < i_class2_one < i_noclass_one |
29 |
| - < i_func1_one < i_func2_one |
30 |
| - < i_class1_two < i_class2_two < i_noclass_two |
31 |
| - < i_func1_two < i_func2_two) |
32 |
| - |
33 |
| - |
34 |
| -def test_module_scope(capsys): |
35 |
| - args = ["-v", "--order-scope=module", fixture_path()] |
| 112 | + # under Windows, the module name is not listed in this case, |
| 113 | + # so do not expect it |
| 114 | + expected = ( |
| 115 | + ".*::Test1::test_one" |
| 116 | + ".*::Test2::test_one" |
| 117 | + ".*::test_one" |
| 118 | + ".*::test1_one" |
| 119 | + ".*::test2_one" |
| 120 | + ".*::Test1::test_two" |
| 121 | + ".*::Test2::test_two" |
| 122 | + ".*::test_two" |
| 123 | + ".*::test1_two" |
| 124 | + ".*::test2_two.*" |
| 125 | + ) |
| 126 | + assert re.match(expected, out.replace("\n", "")), out |
| 127 | + |
| 128 | + |
| 129 | +def test_module_scope(fixture_path, capsys): |
| 130 | + args = ["-v", "--order-scope=module", fixture_path] |
36 | 131 | pytest.main(args, [pytest_order])
|
37 | 132 | out, err = capsys.readouterr()
|
38 |
| - print('module:', out) |
39 |
| - i_class1_one = out.index("test_classes.py::Test1::test_one") |
40 |
| - i_class2_one = out.index("test_classes.py::Test2::test_one") |
41 |
| - i_noclass_one = out.index("test_classes.py::test_one") |
42 |
| - i_func1_one = out.index("test_functions1.py::test_one") |
43 |
| - i_func2_one = out.index("test_functions2.py::test_one") |
44 |
| - i_class1_two = out.index("test_classes.py::Test1::test_two") |
45 |
| - i_class2_two = out.index("test_classes.py::Test2::test_two") |
46 |
| - i_noclass_two = out.index("test_classes.py::test_two") |
47 |
| - i_func1_two = out.index("test_functions1.py::test_two") |
48 |
| - i_func2_two = out.index("test_functions2.py::test_two") |
49 |
| - assert (i_class1_one < i_class2_one < i_noclass_one |
50 |
| - < i_class1_two < i_class2_two < i_noclass_two |
51 |
| - < i_func1_one < i_func1_two |
52 |
| - < i_func2_one < i_func2_two) |
53 |
| - |
54 |
| - |
55 |
| -def test_class_scope(capsys): |
56 |
| - args = ["-v", "--order-scope=class", fixture_path()] |
| 133 | + expected = ( |
| 134 | + ".*test_classes.py::Test1::test_one" |
| 135 | + ".*test_classes.py::Test2::test_one" |
| 136 | + ".*test_classes.py::test_one" |
| 137 | + ".*test_classes.py::Test1::test_two" |
| 138 | + ".*test_classes.py::Test2::test_two" |
| 139 | + ".*test_classes.py::test_two" |
| 140 | + ".*test_functions1.py::test1_one" |
| 141 | + ".*test_functions1.py::test1_two" |
| 142 | + ".*test_functions2.py::test2_one" |
| 143 | + ".*test_functions2.py::test2_two.*" |
| 144 | + ) |
| 145 | + assert re.match(expected, out.replace("\n", "")), out |
| 146 | + |
| 147 | + |
| 148 | +def test_class_scope(fixture_path, capsys): |
| 149 | + args = ["-v", "--order-scope=class", fixture_path] |
57 | 150 | pytest.main(args, [pytest_order])
|
58 | 151 | out, err = capsys.readouterr()
|
59 |
| - print('class:', out) |
60 |
| - i_class1_one = out.index("test_classes.py::Test1::test_one") |
61 |
| - i_class2_one = out.index("test_classes.py::Test2::test_one") |
62 |
| - i_noclass_one = out.index("test_classes.py::test_one") |
63 |
| - i_func1_one = out.index("test_functions1.py::test_one") |
64 |
| - i_func2_one = out.index("test_functions2.py::test_one") |
65 |
| - i_class1_two = out.index("test_classes.py::Test1::test_two") |
66 |
| - i_class2_two = out.index("test_classes.py::Test2::test_two") |
67 |
| - i_noclass_two = out.index("test_classes.py::test_two") |
68 |
| - i_func1_two = out.index("test_functions1.py::test_two") |
69 |
| - i_func2_two = out.index("test_functions2.py::test_two") |
70 |
| - assert (i_class1_one < i_class1_two < i_class2_one < i_class2_two |
71 |
| - < i_noclass_one < i_noclass_two |
72 |
| - < i_func1_one < i_func1_two |
73 |
| - < i_func2_one < i_func2_two) |
| 152 | + expected = ( |
| 153 | + ".*test_classes.py::Test1::test_one" |
| 154 | + ".*test_classes.py::Test1::test_two" |
| 155 | + ".*test_classes.py::Test2::test_one" |
| 156 | + ".*test_classes.py::Test2::test_two" |
| 157 | + ".*test_classes.py::test_one" |
| 158 | + ".*test_classes.py::test_two" |
| 159 | + ".*test_functions1.py::test1_one" |
| 160 | + ".*test_functions1.py::test1_two" |
| 161 | + ".*test_functions2.py::test2_one" |
| 162 | + ".*test_functions2.py::test2_two.*" |
| 163 | + ) |
| 164 | + assert re.match(expected, out.replace("\n", "")), out |
74 | 165 |
|
75 | 166 |
|
76 | 167 | @pytest.mark.skipif(pytest.__version__.startswith(("3.6.", "3.7.")),
|
77 |
| - reason="Warning does not appear in out in pytest < 3.8") |
78 |
| -def test_invalid_scope(capsys): |
79 |
| - args = ["-v", "--order-scope=function", fixture_path()] |
| 168 | + reason="Warning does not appear in output in pytest < 3.8") |
| 169 | +def test_invalid_scope(fixture_path, capsys): |
| 170 | + args = ["-v", "--order-scope=function", fixture_path] |
80 | 171 | pytest.main(args, [pytest_order])
|
81 | 172 | out, err = capsys.readouterr()
|
82 | 173 | assert "UserWarning: Unknown order scope 'function', ignoring it." in out
|
0 commit comments