Skip to content

Commit a8d8de2

Browse files
committed
fix instance case and unittest test case
1 parent 967d79f commit a8d8de2

File tree

2 files changed

+138
-1
lines changed

2 files changed

+138
-1
lines changed

pytest_lazyfixture.py

Lines changed: 15 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,9 @@ def pytest_namespace():
1720

1821
@pytest.hookimpl(tryfirst=True)
1922
def pytest_runtest_setup(item):
23+
if isinstance(item, TestCaseFunction):
24+
return
25+
2026
fixturenames = item.fixturenames
2127
argnames = item._fixtureinfo.argnames
2228

@@ -29,6 +35,14 @@ def pytest_runtest_setup(item):
2935
if is_lazy_fixture(val):
3036
item.callspec.params[param] = item._request.getfixturevalue(val.name)
3137

38+
if isinstance(item.parent, Instance):
39+
def newinstance(self):
40+
return self.obj
41+
42+
item.parent.newinstance = types.MethodType(
43+
newinstance, item.parent
44+
)
45+
3246

3347
def pytest_runtest_call(item):
3448
if hasattr(item, 'funcargs'):

tests/test_lazyfixture.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,129 @@ 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(self):
395+
assert self.var == 15
396+
397+
""")
398+
reprec = testdir.inline_run('-s', '-v')
399+
reprec.assertoutcome(passed=1)
400+
401+
402+
def test_issues12_skip_test_function(testdir):
403+
testdir.makepyfile("""
404+
import pytest
405+
406+
@pytest.fixture
407+
def one():
408+
return 1
409+
410+
@pytest.mark.parametrize('a', [
411+
pytest.mark.skip((pytest.lazy_fixture('one'),), reason='skip')
412+
])
413+
def test_skip1(a):
414+
assert a == 1
415+
416+
@pytest.mark.skip(reason='skip')
417+
@pytest.mark.parametrize('a', [
418+
pytest.lazy_fixture('one')
419+
])
420+
def test_skip2(a):
421+
assert a == 1
422+
""")
423+
reprec = testdir.inline_run('-s', '-v')
424+
reprec.assertoutcome(skipped=2)
425+
426+
427+
def test_issues12_skip_test_method(testdir):
428+
testdir.makepyfile("""
429+
import pytest
430+
431+
class TestModels:
432+
@pytest.fixture
433+
def one(self):
434+
return 1
435+
436+
@pytest.mark.skip(reason='skip this')
437+
@pytest.mark.parametrize('a', [
438+
pytest.lazy_fixture('one')
439+
])
440+
def test_model_a(self, a):
441+
assert a == 1
442+
443+
@pytest.mark.parametrize('a', [
444+
pytest.mark.skip((pytest.lazy_fixture('one'),), reason='skip this')
445+
])
446+
def test_model_b(self, a):
447+
assert a == 1
448+
""")
449+
reprec = testdir.inline_run('-s', '-v')
450+
reprec.assertoutcome(skipped=2)
451+
452+
453+
def test_issues12_lf_as_method_of_test_class(testdir):
454+
testdir.makepyfile("""
455+
import pytest
456+
457+
class TestModels:
458+
@pytest.fixture
459+
def one(self):
460+
return 1
461+
462+
@pytest.mark.parametrize('a', [
463+
pytest.lazy_fixture('one')
464+
])
465+
def test_lf(self, a):
466+
assert a == 1
467+
""")
468+
reprec = testdir.inline_run('-s', '-v')
469+
reprec.assertoutcome(passed=1)
470+
471+
472+
def test_issues13_unittest_testcase_class_should_not_fail(testdir):
473+
testdir.makepyfile("""
474+
import unittest
475+
import pytest
476+
477+
class TestModels(unittest.TestCase):
478+
def test_models(self):
479+
assert True
480+
481+
def test_models_fail(self):
482+
assert False
483+
""")
484+
reprec = testdir.inline_run('-s', '-v')
485+
reprec.assertoutcome(passed=1, failed=1)
486+
487+
365488
def lf(fname):
366489
return lazy_fixture(fname)
367490

0 commit comments

Comments
 (0)