Skip to content

Commit f6ceedd

Browse files
authored
Merge pull request #3636 from RonnyPfannschmidt/fixturetest-examples
[RFC] Fixturetest examples - move test contents to use example scripts for contents
2 parents 5226c7f + 0fd86ec commit f6ceedd

File tree

19 files changed

+246
-229
lines changed

19 files changed

+246
-229
lines changed

src/_pytest/pytester.py

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import pytest
2222
from _pytest.main import Session, EXIT_OK
2323
from _pytest.assertion.rewrite import AssertionRewritingHook
24-
24+
from _pytest.compat import Path
2525

2626
PYTEST_FULLPATH = os.path.abspath(pytest.__file__.rstrip("oc")).replace(
2727
"$py.class", ".py"
@@ -632,19 +632,45 @@ def mkpydir(self, name):
632632
p.ensure("__init__.py")
633633
return p
634634

635-
def copy_example(self, name):
635+
def copy_example(self, name=None):
636636
from . import experiments
637637
import warnings
638638

639639
warnings.warn(experiments.PYTESTER_COPY_EXAMPLE, stacklevel=2)
640640
example_dir = self.request.config.getini("pytester_example_dir")
641641
if example_dir is None:
642642
raise ValueError("pytester_example_dir is unset, can't copy examples")
643-
example_path = self.request.config.rootdir.join(example_dir, name)
643+
example_dir = self.request.config.rootdir.join(example_dir)
644+
645+
for extra_element in self.request.node.iter_markers("pytester_example_path"):
646+
assert extra_element.args
647+
example_dir = example_dir.join(*extra_element.args)
648+
649+
if name is None:
650+
func_name = self.request.function.__name__
651+
maybe_dir = example_dir / func_name
652+
maybe_file = example_dir / (func_name + ".py")
653+
654+
if maybe_dir.isdir():
655+
example_path = maybe_dir
656+
elif maybe_file.isfile():
657+
example_path = maybe_file
658+
else:
659+
raise LookupError(
660+
"{} cant be found as module or package in {}".format(
661+
func_name, example_dir.bestrelpath(self.request.confg.rootdir)
662+
)
663+
)
664+
else:
665+
example_path = example_dir.join(name)
666+
644667
if example_path.isdir() and not example_path.join("__init__.py").isfile():
645668
example_path.copy(self.tmpdir)
669+
return self.tmpdir
646670
elif example_path.isfile():
647-
example_path.copy(self.tmpdir.join(example_path.basename))
671+
result = self.tmpdir.join(example_path.basename)
672+
example_path.copy(result)
673+
return result
648674
else:
649675
raise LookupError("example is not found as a file or directory")
650676

@@ -954,14 +980,16 @@ def getmodulecol(self, source, configargs=(), withinit=False):
954980
same directory to ensure it is a package
955981
956982
"""
957-
kw = {self.request.function.__name__: Source(source).strip()}
958-
path = self.makepyfile(**kw)
983+
if isinstance(source, Path):
984+
path = self.tmpdir.join(str(source))
985+
assert not withinit, "not supported for paths"
986+
else:
987+
kw = {self.request.function.__name__: Source(source).strip()}
988+
path = self.makepyfile(**kw)
959989
if withinit:
960990
self.makepyfile(__init__="#")
961991
self.config = config = self.parseconfigure(path, *configargs)
962-
node = self.getnode(config, path)
963-
964-
return node
992+
return self.getnode(config, path)
965993

966994
def collect_by_name(self, modcol, name):
967995
"""Return the collection node for name from the module collection.

testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in_subdir/sub1/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import pytest
2+
3+
4+
@pytest.fixture
5+
def arg1(request):
6+
with pytest.raises(Exception):
7+
request.getfixturevalue("arg2")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def test_1(arg1):
2+
pass

testing/example_scripts/fixtures/fill_fixtures/test_conftest_funcargs_only_available_in_subdir/sub2/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
import pytest
3+
4+
5+
@pytest.fixture
6+
def arg2(request):
7+
pytest.raises(Exception, "request.getfixturevalue('arg1')")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def test_2(arg2):
2+
pass
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import pytest
2+
3+
4+
@pytest.fixture
5+
def spam():
6+
return "spam"

testing/example_scripts/fixtures/fill_fixtures/test_extend_fixture_conftest_conftest/pkg/__init__.py

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import pytest
2+
3+
4+
@pytest.fixture
5+
def spam(spam):
6+
return spam * 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def test_spam(spam):
2+
assert spam == "spamspam"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import pytest
2+
3+
4+
@pytest.fixture
5+
def spam():
6+
return "spam"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import pytest
2+
3+
4+
@pytest.fixture
5+
def spam(spam):
6+
return spam * 2
7+
8+
9+
def test_spam(spam):
10+
assert spam == "spamspam"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
3+
4+
@pytest.fixture
5+
def spam():
6+
return "spam"
7+
8+
9+
class TestSpam(object):
10+
@pytest.fixture
11+
def spam(self, spam):
12+
return spam * 2
13+
14+
def test_spam(self, spam):
15+
assert spam == "spamspam"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
import pytest
3+
4+
5+
@pytest.fixture
6+
def some(request):
7+
return request.function.__name__
8+
9+
10+
@pytest.fixture
11+
def other(request):
12+
return 42
13+
14+
15+
def test_func(some, other):
16+
pass
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
import pytest
3+
4+
5+
class TestClass(object):
6+
@pytest.fixture
7+
def something(self, request):
8+
return request.instance
9+
10+
def test_method(self, something):
11+
assert something is self
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
3+
4+
@pytest.fixture
5+
def something(request):
6+
return request.function.__name__
7+
8+
9+
class TestClass(object):
10+
def test_method(self, something):
11+
assert something == "test_method"
12+
13+
14+
def test_func(something):
15+
assert something == "test_func"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import pytest
2+
3+
4+
@pytest.fixture
5+
def xyzsomething(request):
6+
return 42
7+
8+
9+
def test_func(some):
10+
pass

0 commit comments

Comments
 (0)