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

Skip __main__.py #232

Merged
merged 3 commits into from
Nov 6, 2023
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
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

- Versions of Python <3.8 are no longer supported. [#217]

- Fix erroneous attempt to import ``__main__.py`` by skipping it [#232]


1.0.0 (2023-08-11)
==================
Expand Down
2 changes: 1 addition & 1 deletion pytest_doctestplus/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def collect(self):
fspath = self.fspath
filepath = self.fspath.basename

if filepath == "setup.py":
if filepath in ("setup.py", "__main__.py"):
return
elif filepath == "conftest.py":
if PYTEST_GE_7_0:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_doctestplus.py
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,24 @@ class MyClass:
assert ("something()\nUNEXPECTED EXCEPTION: NameError" in report.longreprtext) is cont_on_fail


def test_main(testdir):
pkg = testdir.mkdir('pkg')
code = dedent(
'''
def f():
raise RuntimeError("This is a CLI, do not execute module while doctesting")

f()
'''
)
pkg.join('__init__.py').write_text("", "utf-8")
main_path = pkg.join('__main__.py')
main_path.write_text(code, "utf-8")

testdir.inline_run(pkg).assertoutcome(passed=0)
testdir.inline_run(pkg, '--doctest-plus').assertoutcome(passed=0)


def test_ufunc(testdir):
pytest.importorskip('numpy')

Expand Down