Skip to content

[RFC] Fixturetest examples - move test contents to use example scripts for contents #3636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions src/_pytest/pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
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"
Expand Down Expand Up @@ -632,19 +632,45 @@ def mkpydir(self, name):
p.ensure("__init__.py")
return p

def copy_example(self, name):
def copy_example(self, name=None):
from . import experiments
import warnings

warnings.warn(experiments.PYTESTER_COPY_EXAMPLE, stacklevel=2)
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")

Expand Down Expand Up @@ -954,14 +980,16 @@ def getmodulecol(self, source, configargs=(), withinit=False):
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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import pytest


@pytest.fixture
def arg1(request):
with pytest.raises(Exception):
request.getfixturevalue("arg2")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def test_1(arg1):
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

import pytest


@pytest.fixture
def arg2(request):
pytest.raises(Exception, "request.getfixturevalue('arg1')")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def test_2(arg2):
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import pytest


@pytest.fixture
def spam():
return "spam"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import pytest


@pytest.fixture
def spam(spam):
return spam * 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def test_spam(spam):
assert spam == "spamspam"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import pytest


@pytest.fixture
def spam():
return "spam"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pytest


@pytest.fixture
def spam(spam):
return spam * 2


def test_spam(spam):
assert spam == "spamspam"
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pytest


@pytest.fixture
def xyzsomething(request):
return 42


def test_func(some):
pass
Loading