def test_cmdline_python_package(self, pytester: Pytester, monkeypatch) -> None: import warnings monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", False) path = pytester.mkpydir("tpkg") path.joinpath("test_hello.py").write_text("def test_hello(): pass") path.joinpath("test_world.py").write_text("def test_world(): pass") result = pytester.runpytest("--pyargs", "tpkg") assert result.ret == 0 result.stdout.fnmatch_lines(["*2 passed*"]) result = pytester.runpytest("--pyargs", "tpkg.test_hello", syspathinsert=True) assert result.ret == 0 result.stdout.fnmatch_lines(["*1 passed*"]) empty_package = pytester.mkpydir("empty_package") monkeypatch.setenv("PYTHONPATH", str(empty_package), prepend=os.pathsep) # the path which is not a package raises a warning on pypy; # no idea why only pypy and not normal python warn about it here with warnings.catch_warnings(): warnings.simplefilter("ignore", ImportWarning) result = pytester.runpytest("--pyargs", ".") assert result.ret == 0 result.stdout.fnmatch_lines(["*2 passed*"]) monkeypatch.setenv("PYTHONPATH", str(pytester), prepend=os.pathsep) result = pytester.runpytest("--pyargs", "tpkg.test_missing", syspathinsert=True) assert result.ret != 0 result.stderr.fnmatch_lines(["*not*found*test_missing*"])
def test_import_plugin_dotted_name(self, pytester: Pytester, pytestpm: PytestPluginManager) -> None: pytest.raises(ImportError, pytestpm.import_plugin, "qweqwex.y") pytest.raises(ImportError, pytestpm.import_plugin, "pytest_qweqwex.y") pytester.syspathinsert() pytester.mkpydir("pkg").joinpath("plug.py").write_text("x=3") pluginname = "pkg.plug" pytestpm.import_plugin(pluginname) mod = pytestpm.get_plugin("pkg.plug") assert mod.x == 3
def test_setup_only_available_in_subdir(pytester: Pytester) -> None: sub1 = pytester.mkpydir("sub1") sub2 = pytester.mkpydir("sub2") sub1.joinpath("conftest.py").write_text( textwrap.dedent( """\ import pytest def pytest_runtest_setup(item): assert item.fspath.purebasename == "test_in_sub1" def pytest_runtest_call(item): assert item.fspath.purebasename == "test_in_sub1" def pytest_runtest_teardown(item): assert item.fspath.purebasename == "test_in_sub1" """ ) ) sub2.joinpath("conftest.py").write_text( textwrap.dedent( """\ import pytest def pytest_runtest_setup(item): assert item.fspath.purebasename == "test_in_sub2" def pytest_runtest_call(item): assert item.fspath.purebasename == "test_in_sub2" def pytest_runtest_teardown(item): assert item.fspath.purebasename == "test_in_sub2" """ ) ) sub1.joinpath("test_in_sub1.py").write_text("def test_1(): pass") sub2.joinpath("test_in_sub2.py").write_text("def test_2(): pass") result = pytester.runpytest("-v", "-s") result.assert_outcomes(passed=2)
def test_package_ordering(pytester: Pytester) -> None: """ . └── root ├── Test_root.py ├── __init__.py ├── sub1 │ ├── Test_sub1.py │ └── __init__.py └── sub2 └── test └── test_sub2.py """ pytester.makeini( """ [pytest] python_files=*.py """ ) root = pytester.mkpydir("root") sub1 = root.joinpath("sub1") sub1.mkdir() sub1.joinpath("__init__.py").touch() sub2 = root.joinpath("sub2") sub2_test = sub2.joinpath("test") sub2_test.mkdir(parents=True) root.joinpath("Test_root.py").write_text("def test_1(): pass") sub1.joinpath("Test_sub1.py").write_text("def test_2(): pass") sub2_test.joinpath("test_sub2.py").write_text("def test_3(): pass") # Execute from . result = pytester.runpytest("-v", "-s") result.assert_outcomes(passed=3)
def test_doctestmodule_external_and_issue116(self, pytester: Pytester): p = pytester.mkpydir("hello") p.joinpath("__init__.py").write_text( textwrap.dedent( """\ def somefunc(): ''' >>> i = 0 >>> i + 1 2 ''' """ ) ) result = pytester.runpytest(p, "--doctest-modules") result.stdout.fnmatch_lines( [ "003 *>>> i = 0", "004 *>>> i + 1", "*Expected:", "* 2", "*Got:", "* 1", "*:4: DocTestFailure", ] )
def test_importerror(pytester: Pytester) -> None: p = pytester.mkpydir("package") p.joinpath("a.py").write_text( textwrap.dedent( """\ import doesnotexist x = 1 """ ) ) pytester.path.joinpath("test_importerror.py").write_text( textwrap.dedent( """\ def test_importerror(monkeypatch): monkeypatch.setattr('package.a.x', 2) """ ) ) result = pytester.runpytest() result.stdout.fnmatch_lines( """ *import error in package.a: No module named 'doesnotexist'* """ )
def test_link_resolve(pytester: Pytester) -> None: """See: https://github.com/pytest-dev/pytest/issues/5965.""" sub1 = pytester.mkpydir("sub1") p = sub1.joinpath("test_foo.py") p.write_text( textwrap.dedent( """ import pytest def test_foo(): raise AssertionError() """ ) ) subst = subst_path_linux if sys.platform == "win32": subst = subst_path_windows with subst(p) as subst_p: result = pytester.runpytest(str(subst_p), "-v") # i.e.: Make sure that the error is reported as a relative path, not as a # resolved path. # See: https://github.com/pytest-dev/pytest/issues/5965 stdout = result.stdout.str() assert "sub1/test_foo.py" not in stdout # i.e.: Expect drive on windows because we just have drive:filename, whereas # we expect a relative path on Linux. expect = f"*{subst_p}*" if sys.platform == "win32" else "*sub2/test_foo.py*" result.stdout.fnmatch_lines([expect])
def test_pyargs_importerror(self, pytester: Pytester, monkeypatch) -> None: monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", False) path = pytester.mkpydir("tpkg") path.joinpath("test_hello.py").write_text("raise ImportError") result = pytester.runpytest("--pyargs", "tpkg.test_hello", syspathinsert=True) assert result.ret != 0 result.stdout.fnmatch_lines(["collected*0*items*/*1*error"])
def test_pyargs_only_imported_once(self, pytester: Pytester) -> None: pkg = pytester.mkpydir("foo") pkg.joinpath("test_foo.py").write_text( "print('hello from test_foo')\ndef test(): pass") pkg.joinpath("conftest.py").write_text( "def pytest_configure(config): print('configuring')") result = pytester.runpytest("--pyargs", "foo.test_foo", "-s", syspathinsert=True) # should only import once assert result.outlines.count("hello from test_foo") == 1 # should only configure once assert result.outlines.count("configuring") == 1
def test_package_with_modules(pytester: Pytester) -> None: """ . └── root ├── __init__.py ├── sub1 │ ├── __init__.py │ └── sub1_1 │ ├── __init__.py │ └── test_in_sub1.py └── sub2 └── test └── test_in_sub2.py """ root = pytester.mkpydir("root") sub1 = root.joinpath("sub1") sub1_test = sub1.joinpath("sub1_1") sub1_test.mkdir(parents=True) for d in (sub1, sub1_test): d.joinpath("__init__.py").touch() sub2 = root.joinpath("sub2") sub2_test = sub2.joinpath("test") sub2_test.mkdir(parents=True) sub1_test.joinpath("test_in_sub1.py").write_text("def test_1(): pass") sub2_test.joinpath("test_in_sub2.py").write_text("def test_2(): pass") # Execute from . result = pytester.runpytest("-v", "-s") result.assert_outcomes(passed=2) # Execute from . with one argument "root" result = pytester.runpytest("-v", "-s", "root") result.assert_outcomes(passed=2) # Chdir into package's root and execute with no args os.chdir(root) result = pytester.runpytest("-v", "-s") result.assert_outcomes(passed=2)