Skip to content

Commit dfb20dd

Browse files
authored
Merge pull request #14 from TvoroG/issues12
Issues12
2 parents 967d79f + b527dfc commit dfb20dd

File tree

2 files changed

+148
-1
lines changed

2 files changed

+148
-1
lines changed

pytest_lazyfixture.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
# -*- coding: utf-8 -*-
2-
import py
32
import os
43
import sys
4+
import types
55
from collections import defaultdict
6+
import py
67
import pytest
78
from _pytest.fixtures import scopenum_function
9+
from _pytest.python import Instance
10+
from _pytest.unittest import TestCaseFunction
811

912

1013
PY3 = sys.version_info[0] == 3
@@ -17,6 +20,12 @@ def pytest_namespace():
1720

1821
@pytest.hookimpl(tryfirst=True)
1922
def pytest_runtest_setup(item):
23+
if isinstance(item, TestCaseFunction):
24+
return
25+
26+
if isinstance(item.parent, Instance):
27+
_patch_instance(item)
28+
2029
fixturenames = item.fixturenames
2130
argnames = item._fixtureinfo.argnames
2231

@@ -121,6 +130,18 @@ def get_nodeid(module, rootdir):
121130
return relpath
122131

123132

133+
def _patch_instance(item):
134+
obj = Instance.newinstance(item.parent)
135+
item.obj = item._getobj()
136+
137+
def newinstance(self):
138+
return obj
139+
140+
item.parent.newinstance = types.MethodType(
141+
newinstance, item.parent
142+
)
143+
144+
124145
def lazy_fixture(names):
125146
if isinstance(names, string_type):
126147
return LazyFixture(names)

tests/test_lazyfixture.py

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,132 @@ def test_some(arg1):
362362
reprec.assertoutcome(passed=1)
363363

364364

365+
def test_issues10_xfail(testdir):
366+
testdir.makepyfile("""
367+
import pytest
368+
def division(a, b):
369+
return a / b
370+
371+
@pytest.fixture(params=[0])
372+
def zero(request):
373+
return request.param
374+
375+
@pytest.mark.parametrize(('a', 'b'), [
376+
pytest.mark.xfail((1, pytest.lazy_fixture('zero')), reason=ZeroDivisionError)
377+
])
378+
def test_division(a, b):
379+
division(a, b)
380+
""")
381+
reprec = testdir.inline_run('-s', '-v')
382+
reprec.assertoutcome(skipped=1)
383+
384+
385+
def test_issues11_autouse_fixture_in_test_class(testdir):
386+
testdir.makepyfile("""
387+
import pytest
388+
389+
class TestModels(object):
390+
@pytest.fixture(autouse=True)
391+
def setup(self):
392+
self.var = 15
393+
394+
def test_model_a(self):
395+
assert self.var == 15
396+
397+
def test_model_b(self):
398+
assert self.var == 15
399+
400+
""")
401+
reprec = testdir.inline_run('-s', '-v')
402+
reprec.assertoutcome(passed=2)
403+
404+
405+
def test_issues12_skip_test_function(testdir):
406+
testdir.makepyfile("""
407+
import pytest
408+
409+
@pytest.fixture
410+
def one():
411+
return 1
412+
413+
@pytest.mark.parametrize('a', [
414+
pytest.mark.skip((pytest.lazy_fixture('one'),), reason='skip')
415+
])
416+
def test_skip1(a):
417+
assert a == 1
418+
419+
@pytest.mark.skip(reason='skip')
420+
@pytest.mark.parametrize('a', [
421+
pytest.lazy_fixture('one')
422+
])
423+
def test_skip2(a):
424+
assert a == 1
425+
""")
426+
reprec = testdir.inline_run('-s', '-v')
427+
reprec.assertoutcome(skipped=2)
428+
429+
430+
def test_issues12_skip_test_method(testdir):
431+
testdir.makepyfile("""
432+
import pytest
433+
434+
class TestModels:
435+
@pytest.fixture
436+
def one(self):
437+
return 1
438+
439+
@pytest.mark.skip(reason='skip this')
440+
@pytest.mark.parametrize('a', [
441+
pytest.lazy_fixture('one')
442+
])
443+
def test_model_a(self, a):
444+
assert a == 1
445+
446+
@pytest.mark.parametrize('a', [
447+
pytest.mark.skip((pytest.lazy_fixture('one'),), reason='skip this')
448+
])
449+
def test_model_b(self, a):
450+
assert a == 1
451+
""")
452+
reprec = testdir.inline_run('-s', '-v')
453+
reprec.assertoutcome(skipped=2)
454+
455+
456+
def test_issues12_lf_as_method_of_test_class(testdir):
457+
testdir.makepyfile("""
458+
import pytest
459+
460+
class TestModels:
461+
@pytest.fixture
462+
def one(self):
463+
return 1
464+
465+
@pytest.mark.parametrize('a', [
466+
pytest.lazy_fixture('one')
467+
])
468+
def test_lf(self, a):
469+
assert a == 1
470+
""")
471+
reprec = testdir.inline_run('-s', '-v')
472+
reprec.assertoutcome(passed=1)
473+
474+
475+
def test_issues13_unittest_testcase_class_should_not_fail(testdir):
476+
testdir.makepyfile("""
477+
import unittest
478+
import pytest
479+
480+
class TestModels(unittest.TestCase):
481+
def test_models(self):
482+
assert True
483+
484+
def test_models_fail(self):
485+
assert False
486+
""")
487+
reprec = testdir.inline_run('-s', '-v')
488+
reprec.assertoutcome(passed=1, failed=1)
489+
490+
365491
def lf(fname):
366492
return lazy_fixture(fname)
367493

0 commit comments

Comments
 (0)