move some fill fixture acceptance tests contents to the examples script folder

This commit is contained in:
Ronny Pfannschmidt 2018-06-29 10:58:33 +02:00
parent 4ae7e9788c
commit 0fd86ec8a8
19 changed files with 170 additions and 160 deletions

View File

@ -21,7 +21,7 @@ import py
import pytest
from _pytest.main import Session, EXIT_OK
from _pytest.assertion.rewrite import AssertionRewritingHook
from _pytest.compat import Path
PYTEST_FULLPATH = os.path.abspath(pytest.__file__.rstrip("oc")).replace(
"$py.class", ".py"
@ -632,7 +632,7 @@ class Testdir(object):
p.ensure("__init__.py")
return p
def copy_example(self, name):
def copy_example(self, name=None):
from . import experiments
import warnings
@ -640,11 +640,37 @@ class Testdir(object):
example_dir = self.request.config.getini("pytester_example_dir")
if example_dir is None:
raise ValueError("pytester_example_dir is unset, can't copy examples")
example_path = self.request.config.rootdir.join(example_dir, name)
example_dir = self.request.config.rootdir.join(example_dir)
for extra_element in self.request.node.iter_markers("pytester_example_path"):
assert extra_element.args
example_dir = example_dir.join(*extra_element.args)
if name is None:
func_name = self.request.function.__name__
maybe_dir = example_dir / func_name
maybe_file = example_dir / (func_name + ".py")
if maybe_dir.isdir():
example_path = maybe_dir
elif maybe_file.isfile():
example_path = maybe_file
else:
raise LookupError(
"{} cant be found as module or package in {}".format(
func_name, example_dir.bestrelpath(self.request.confg.rootdir)
)
)
else:
example_path = example_dir.join(name)
if example_path.isdir() and not example_path.join("__init__.py").isfile():
example_path.copy(self.tmpdir)
return self.tmpdir
elif example_path.isfile():
example_path.copy(self.tmpdir.join(example_path.basename))
result = self.tmpdir.join(example_path.basename)
example_path.copy(result)
return result
else:
raise LookupError("example is not found as a file or directory")
@ -954,14 +980,16 @@ class Testdir(object):
same directory to ensure it is a package
"""
kw = {self.request.function.__name__: Source(source).strip()}
path = self.makepyfile(**kw)
if isinstance(source, Path):
path = self.tmpdir.join(str(source))
assert not withinit, "not supported for paths"
else:
kw = {self.request.function.__name__: Source(source).strip()}
path = self.makepyfile(**kw)
if withinit:
self.makepyfile(__init__="#")
self.config = config = self.parseconfigure(path, *configargs)
node = self.getnode(config, path)
return node
return self.getnode(config, path)
def collect_by_name(self, modcol, name):
"""Return the collection node for name from the module collection.

View File

@ -0,0 +1,7 @@
import pytest
@pytest.fixture
def arg1(request):
with pytest.raises(Exception):
request.getfixturevalue("arg2")

View File

@ -0,0 +1,7 @@
import pytest
@pytest.fixture
def arg2(request):
pytest.raises(Exception, "request.getfixturevalue('arg1')")

View File

@ -0,0 +1,6 @@
import pytest
@pytest.fixture
def spam():
return "spam"

View File

@ -0,0 +1,6 @@
import pytest
@pytest.fixture
def spam(spam):
return spam * 2

View File

@ -0,0 +1,2 @@
def test_spam(spam):
assert spam == "spamspam"

View File

@ -0,0 +1,6 @@
import pytest
@pytest.fixture
def spam():
return "spam"

View File

@ -0,0 +1,10 @@
import pytest
@pytest.fixture
def spam(spam):
return spam * 2
def test_spam(spam):
assert spam == "spamspam"

View File

@ -0,0 +1,15 @@
import pytest
@pytest.fixture
def spam():
return "spam"
class TestSpam(object):
@pytest.fixture
def spam(self, spam):
return spam * 2
def test_spam(self, spam):
assert spam == "spamspam"

View File

@ -0,0 +1,16 @@
import pytest
@pytest.fixture
def some(request):
return request.function.__name__
@pytest.fixture
def other(request):
return 42
def test_func(some, other):
pass

View File

@ -0,0 +1,11 @@
import pytest
class TestClass(object):
@pytest.fixture
def something(self, request):
return request.instance
def test_method(self, something):
assert something is self

View File

@ -0,0 +1,15 @@
import pytest
@pytest.fixture
def something(request):
return request.function.__name__
class TestClass(object):
def test_method(self, something):
assert something == "test_method"
def test_func(something):
assert something == "test_func"

View File

@ -0,0 +1,10 @@
import pytest
@pytest.fixture
def xyzsomething(request):
return 42
def test_func(some):
pass

View File

@ -5,6 +5,7 @@ import pytest
from _pytest.pytester import get_public_names
from _pytest.fixtures import FixtureLookupError, FixtureRequest
from _pytest import fixtures
from _pytest.compat import Path
def test_getfuncargnames():
@ -40,45 +41,27 @@ def test_getfuncargnames():
assert fixtures.getfuncargnames(A.static, cls=A) == ("arg1", "arg2")
@pytest.mark.pytester_example_path("fixtures/fill_fixtures")
class TestFillFixtures(object):
def test_fillfuncargs_exposed(self):
# used by oejskit, kept for compatibility
assert pytest._fillfuncargs == fixtures.fillfixtures
def test_funcarg_lookupfails(self, testdir):
testdir.makepyfile(
"""
import pytest
@pytest.fixture
def xyzsomething(request):
return 42
def test_func(some):
pass
"""
)
testdir.copy_example()
result = testdir.runpytest() # "--collect-only")
assert result.ret != 0
result.stdout.fnmatch_lines(
["*def test_func(some)*", "*fixture*some*not found*", "*xyzsomething*"]
"""
*def test_func(some)*
*fixture*some*not found*
*xyzsomething*
"""
)
def test_funcarg_basic(self, testdir):
item = testdir.getitem(
"""
import pytest
@pytest.fixture
def some(request):
return request.function.__name__
@pytest.fixture
def other(request):
return 42
def test_func(some, other):
pass
"""
)
testdir.copy_example()
item = testdir.getitem(Path("test_funcarg_basic.py"))
fixtures.fillfixtures(item)
del item.funcargs["request"]
assert len(get_public_names(item.funcargs)) == 2
@ -86,155 +69,39 @@ class TestFillFixtures(object):
assert item.funcargs["other"] == 42
def test_funcarg_lookup_modulelevel(self, testdir):
testdir.makepyfile(
"""
import pytest
@pytest.fixture
def something(request):
return request.function.__name__
class TestClass(object):
def test_method(self, something):
assert something == "test_method"
def test_func(something):
assert something == "test_func"
"""
)
testdir.copy_example()
reprec = testdir.inline_run()
reprec.assertoutcome(passed=2)
def test_funcarg_lookup_classlevel(self, testdir):
p = testdir.makepyfile(
"""
import pytest
class TestClass(object):
@pytest.fixture
def something(self, request):
return request.instance
def test_method(self, something):
assert something is self
"""
)
p = testdir.copy_example()
result = testdir.runpytest(p)
result.stdout.fnmatch_lines(["*1 passed*"])
def test_conftest_funcargs_only_available_in_subdir(self, testdir):
sub1 = testdir.mkpydir("sub1")
sub2 = testdir.mkpydir("sub2")
sub1.join("conftest.py").write(
_pytest._code.Source(
"""
import pytest
@pytest.fixture
def arg1(request):
pytest.raises(Exception, "request.getfixturevalue('arg2')")
"""
)
)
sub2.join("conftest.py").write(
_pytest._code.Source(
"""
import pytest
@pytest.fixture
def arg2(request):
pytest.raises(Exception, "request.getfixturevalue('arg1')")
"""
)
)
sub1.join("test_in_sub1.py").write("def test_1(arg1): pass")
sub2.join("test_in_sub2.py").write("def test_2(arg2): pass")
testdir.copy_example()
result = testdir.runpytest("-v")
result.assert_outcomes(passed=2)
def test_extend_fixture_module_class(self, testdir):
testfile = testdir.makepyfile(
"""
import pytest
@pytest.fixture
def spam():
return 'spam'
class TestSpam(object):
@pytest.fixture
def spam(self, spam):
return spam * 2
def test_spam(self, spam):
assert spam == 'spamspam'
"""
)
testfile = testdir.copy_example()
result = testdir.runpytest()
result.stdout.fnmatch_lines(["*1 passed*"])
result = testdir.runpytest(testfile)
result.stdout.fnmatch_lines(["*1 passed*"])
def test_extend_fixture_conftest_module(self, testdir):
testdir.makeconftest(
"""
import pytest
@pytest.fixture
def spam():
return 'spam'
"""
)
testfile = testdir.makepyfile(
"""
import pytest
@pytest.fixture
def spam(spam):
return spam * 2
def test_spam(spam):
assert spam == 'spamspam'
"""
)
p = testdir.copy_example()
result = testdir.runpytest()
result.stdout.fnmatch_lines(["*1 passed*"])
result = testdir.runpytest(testfile)
result = testdir.runpytest(next(p.visit("test_*.py")))
result.stdout.fnmatch_lines(["*1 passed*"])
def test_extend_fixture_conftest_conftest(self, testdir):
testdir.makeconftest(
"""
import pytest
@pytest.fixture
def spam():
return 'spam'
"""
)
pkg = testdir.mkpydir("pkg")
pkg.join("conftest.py").write(
_pytest._code.Source(
"""
import pytest
@pytest.fixture
def spam(spam):
return spam * 2
"""
)
)
testfile = pkg.join("test_spam.py")
testfile.write(
_pytest._code.Source(
"""
def test_spam(spam):
assert spam == "spamspam"
"""
)
)
p = testdir.copy_example()
result = testdir.runpytest()
result.stdout.fnmatch_lines(["*1 passed*"])
result = testdir.runpytest(testfile)
result = testdir.runpytest(next(p.visit("test_*.py")))
result.stdout.fnmatch_lines(["*1 passed*"])
def test_extend_fixture_conftest_plugin(self, testdir):