1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
from .util import get_source, unmagic_tester
def test_autouse_module_fixture():
@get_source
def test_py():
from unmagic import autouse, fixture, get_request
def test_one():
pass
def test_two():
pass
def test_three():
pass
@fixture(scope="session")
def ss_tracer():
traces = []
yield traces
print("\n", " ".join(traces))
@fixture
def test_name():
name = get_request().node.name.replace("test_", "")
yield
ss_tracer().append(name)
autouse(test_name, __file__)
pytester = unmagic_tester()
pytester.makepyfile(test_py)
result = pytester.runpytest("-s")
result.stdout.fnmatch_lines([
" one two three"
])
result.assert_outcomes(passed=3)
def test_autouse_package_fixture():
@get_source
def fix_py():
from unmagic import fixture
@fixture(scope="session")
def ss_tracer():
traces = []
yield traces
print("\n", " ".join(traces))
@get_source
def init_py():
from unmagic import autouse, fixture, get_request
from fix import ss_tracer
@fixture(scope="package")
def pkg_fix():
name = get_request().node.nodeid.replace("/", ".")
traces = ss_tracer()
traces.append(f"{name}-a")
yield
traces.append(f"{name}-z")
autouse(pkg_fix, __file__)
@get_source
def mod_py():
from unmagic import fixture
from fix import ss_tracer
@fixture
def modname():
yield __name__.rsplit(".", 1)[-1].replace("test_mod", "m")
def test_one():
ss_tracer().append(f"{modname()}.t1")
def test_two():
ss_tracer().append(f"{modname()}.t2")
pytester = unmagic_tester()
(pytester.path / "pkg/sub").mkdir(parents=True)
(pytester.path / "pkg/sub/__init__.py").write_text(init_py)
(pytester.path / "pkg/sub/test_mod0.py").write_text(mod_py)
(pytester.path / "pkg/__init__.py").write_text(init_py)
(pytester.path / "pkg/test_mod1.py").write_text(mod_py)
(pytester.path / "pkg/test_mod2.py").write_text(mod_py)
(pytester.path / "pkg/up").mkdir()
(pytester.path / "pkg/up/__init__.py").write_text(init_py)
(pytester.path / "pkg/up/test_mod3.py").write_text(mod_py)
(pytester.path / "test_mod4.py").write_text(mod_py)
(pytester.path / "fix.py").write_text(fix_py)
result = pytester.runpytest("-s")
result.stdout.fnmatch_lines([
" pkg-a"
" pkg.sub-a m0.t1 m0.t2 pkg.sub-z"
" m1.t1 m1.t2 m2.t1 m2.t2"
" pkg.up-a m3.t1 m3.t2 pkg.up-z"
" pkg-z"
" m4.t1 m4.t2"
])
result.assert_outcomes(passed=10)
def test_use_on_autouse_fixture():
@get_source
def test_py():
import pytest
from unmagic import fixture
traces = []
@fixture(scope="session")
def trace():
traces.append("tracing...")
yield
with pytest.raises(TypeError, match="Cannot apply @use to autouse"):
@trace
@fixture(autouse=True)
def use_autouse():
traces.append("autoused")
yield
def test():
assert traces == ["autoused"]
pytester = unmagic_tester()
pytester.makepyfile(test_py)
result = pytester.runpytest("-s")
result.assert_outcomes(passed=1)
def test_autouse_context_manager():
@get_source
def test_py():
from contextlib import contextmanager
from unmagic import fixture
traces = []
@contextmanager
def context():
traces.append("a")
yield
traces.append("z")
print()
print(' '.join(traces))
fixture(context, scope="module", autouse=__file__)
def test_one():
traces.append("1")
def test_two():
traces.append("2")
pytester = unmagic_tester()
pytester.makepyfile(test_py)
result = pytester.runpytest("-s")
result.stdout.fnmatch_lines(["a 1 2 z"])
result.assert_outcomes(passed=2)
def test_autouse_conftest_fixture():
@get_source
def test_py():
from conftest import ss_tracer
def test_one():
ss_tracer().append("t1")
def test_two():
ss_tracer().append("t2")
pytester = unmagic_tester()
pytester.makepyfile(conftest=plug_py, test_it=test_py)
result = pytester.runpytest("-s")
result.stdout.fnmatch_lines([" a t1 z a t2 z"])
result.assert_outcomes(passed=2)
def test_autouse_plugin_fixture():
@get_source
def test_py():
from plug import ss_tracer
def test_one():
ss_tracer().append("t1")
def test_two():
ss_tracer().append("t2")
pytester = unmagic_tester()
pytester.syspathinsert(pytester.path)
pytester.makepyfile(plug=plug_py, test_it=test_py)
result = pytester.runpytest("-s", "-pplug")
result.stdout.fnmatch_lines([" a t1 z a t2 z"])
result.assert_outcomes(passed=2)
def test_autouse_warns_in_runtest_phase():
@get_source
def test_py():
from unmagic import autouse
from conftest import ss_tracer, autofix
def test_one():
autouse(autofix, True)
ss_tracer().append("t1")
conftest = plug_py.replace("(autouse=True)", "")
pytester = unmagic_tester()
pytester.makepyfile(conftest=conftest, test_it=test_py)
result = pytester.runpytest("-s")
result.stdout.fnmatch_lines([
" t1",
"*UserWarning: autouse fixture registered while running tests*",
])
result.assert_outcomes(passed=1)
@get_source
def plug_py():
from unmagic import fixture
@fixture(scope="session")
def ss_tracer():
traces = []
yield traces
print("\n", " ".join(traces))
@fixture(autouse=True)
def autofix():
traces = ss_tracer()
traces.append("a")
yield
traces.append("z")
|