Skip to content

Commit

Permalink
Merge pull request #14 from TvoroG/issues12
Browse files Browse the repository at this point in the history
Issues12
  • Loading branch information
TvoroG authored Jan 31, 2017
2 parents 967d79f + b527dfc commit dfb20dd
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 1 deletion.
23 changes: 22 additions & 1 deletion pytest_lazyfixture.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# -*- coding: utf-8 -*-
import py
import os
import sys
import types
from collections import defaultdict
import py
import pytest
from _pytest.fixtures import scopenum_function
from _pytest.python import Instance
from _pytest.unittest import TestCaseFunction


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

@pytest.hookimpl(tryfirst=True)
def pytest_runtest_setup(item):
if isinstance(item, TestCaseFunction):
return

if isinstance(item.parent, Instance):
_patch_instance(item)

fixturenames = item.fixturenames
argnames = item._fixtureinfo.argnames

Expand Down Expand Up @@ -121,6 +130,18 @@ def get_nodeid(module, rootdir):
return relpath


def _patch_instance(item):
obj = Instance.newinstance(item.parent)
item.obj = item._getobj()

def newinstance(self):
return obj

item.parent.newinstance = types.MethodType(
newinstance, item.parent
)


def lazy_fixture(names):
if isinstance(names, string_type):
return LazyFixture(names)
Expand Down
126 changes: 126 additions & 0 deletions tests/test_lazyfixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,132 @@ def test_some(arg1):
reprec.assertoutcome(passed=1)


def test_issues10_xfail(testdir):
testdir.makepyfile("""
import pytest
def division(a, b):
return a / b
@pytest.fixture(params=[0])
def zero(request):
return request.param
@pytest.mark.parametrize(('a', 'b'), [
pytest.mark.xfail((1, pytest.lazy_fixture('zero')), reason=ZeroDivisionError)
])
def test_division(a, b):
division(a, b)
""")
reprec = testdir.inline_run('-s', '-v')
reprec.assertoutcome(skipped=1)


def test_issues11_autouse_fixture_in_test_class(testdir):
testdir.makepyfile("""
import pytest
class TestModels(object):
@pytest.fixture(autouse=True)
def setup(self):
self.var = 15
def test_model_a(self):
assert self.var == 15
def test_model_b(self):
assert self.var == 15
""")
reprec = testdir.inline_run('-s', '-v')
reprec.assertoutcome(passed=2)


def test_issues12_skip_test_function(testdir):
testdir.makepyfile("""
import pytest
@pytest.fixture
def one():
return 1
@pytest.mark.parametrize('a', [
pytest.mark.skip((pytest.lazy_fixture('one'),), reason='skip')
])
def test_skip1(a):
assert a == 1
@pytest.mark.skip(reason='skip')
@pytest.mark.parametrize('a', [
pytest.lazy_fixture('one')
])
def test_skip2(a):
assert a == 1
""")
reprec = testdir.inline_run('-s', '-v')
reprec.assertoutcome(skipped=2)


def test_issues12_skip_test_method(testdir):
testdir.makepyfile("""
import pytest
class TestModels:
@pytest.fixture
def one(self):
return 1
@pytest.mark.skip(reason='skip this')
@pytest.mark.parametrize('a', [
pytest.lazy_fixture('one')
])
def test_model_a(self, a):
assert a == 1
@pytest.mark.parametrize('a', [
pytest.mark.skip((pytest.lazy_fixture('one'),), reason='skip this')
])
def test_model_b(self, a):
assert a == 1
""")
reprec = testdir.inline_run('-s', '-v')
reprec.assertoutcome(skipped=2)


def test_issues12_lf_as_method_of_test_class(testdir):
testdir.makepyfile("""
import pytest
class TestModels:
@pytest.fixture
def one(self):
return 1
@pytest.mark.parametrize('a', [
pytest.lazy_fixture('one')
])
def test_lf(self, a):
assert a == 1
""")
reprec = testdir.inline_run('-s', '-v')
reprec.assertoutcome(passed=1)


def test_issues13_unittest_testcase_class_should_not_fail(testdir):
testdir.makepyfile("""
import unittest
import pytest
class TestModels(unittest.TestCase):
def test_models(self):
assert True
def test_models_fail(self):
assert False
""")
reprec = testdir.inline_run('-s', '-v')
reprec.assertoutcome(passed=1, failed=1)


def lf(fname):
return lazy_fixture(fname)

Expand Down

0 comments on commit dfb20dd

Please sign in to comment.