Skip to content
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

Pytester runexamples #3623

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 12 additions & 0 deletions src/_pytest/pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def pytest_addoption(parser):
),
)

parser.addini(
"pytester_example_dir", help="directory to take the pytester example files from"
)


def pytest_configure(config):
if config.getvalue("lsof"):
Expand Down Expand Up @@ -628,6 +632,14 @@ def mkpydir(self, name):
p.ensure("__init__.py")
return p

def copy_example(self, name):
example_dir = self.request.config.getini("pytester_example_dir")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably makes sense to raise an error if pytester_example_dir is None.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we mark this as experimental?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes we should - what mechanism do we want to use for that?

example_path = self.request.config.rootdir.join(example_dir, name)
if example_path.isdir() and not example_path.join("__init__.py").isfile():
example_path.copy(self.tmpdir)
else:
example_path.copy(self.tmpdir.join(example_path.basename))

Session = Session

def getnode(self, config, arg):
Expand Down
21 changes: 2 additions & 19 deletions testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@

class TestGeneralUsage(object):
def test_config_error(self, testdir):
testdir.makeconftest(
"""
def pytest_configure(config):
import pytest
raise pytest.UsageError("hello")
"""
)
testdir.copy_example("conftest_usageerror/conftest.py")
result = testdir.runpytest(testdir.tmpdir)
assert result.ret != 0
result.stderr.fnmatch_lines(["*ERROR: hello"])
Expand Down Expand Up @@ -170,18 +164,7 @@ def pytest_collect_directory():
result.stdout.fnmatch_lines(["*1 skip*"])

def test_issue88_initial_file_multinodes(self, testdir):
testdir.makeconftest(
"""
import pytest
class MyFile(pytest.File):
def collect(self):
return [MyItem("hello", parent=self)]
def pytest_collect_file(path, parent):
return MyFile(path, parent)
class MyItem(pytest.Item):
pass
"""
)
testdir.copy_example("issue88_initial_file_multinodes")
p = testdir.makepyfile("def test_hello(): pass")
result = testdir.runpytest(p, "--collect-only")
result.stdout.fnmatch_lines(["*MyFile*test_issue88*", "*Module*test_issue88*"])
Expand Down
4 changes: 4 additions & 0 deletions testing/example_scripts/conftest_usageerror/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def pytest_configure(config):
import pytest

raise pytest.UsageError("hello")
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pytest


class MyFile(pytest.File):
def collect(self):
return [MyItem("hello", parent=self)]


def pytest_collect_file(path, parent):
return MyFile(path, parent)


class MyItem(pytest.Item):
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def test_hello():
pass
3 changes: 3 additions & 0 deletions testing/examples/test_issue519.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def test_510(testdir):
testdir.copy_example("issue_519.py")
testdir.runpytest("issue_519.py")
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ filterwarnings =
ignore:.*type argument to addoption.*:DeprecationWarning
# produced by python >=3.5 on execnet (pytest-xdist)
ignore:.*inspect.getargspec.*deprecated, use inspect.signature.*:DeprecationWarning

pytester_example_dir = testing/example_scripts
[flake8]
max-line-length = 120
ignore = E203,W503