def test_issue4445_rewrite(self, pytester: Pytester, capwarn) -> None: """#4445: Make sure the warning points to a reasonable location See origin of _issue_warning_captured at: _pytest.assertion.rewrite.py:241 """ pytester.makepyfile(some_mod="") conftest = pytester.makeconftest( """ import some_mod import pytest pytest.register_assert_rewrite("some_mod") """ ) pytester.parseconfig() # with stacklevel=5 the warning originates from register_assert_rewrite # function in the created conftest.py assert len(capwarn.captured) == 1 warning, location = capwarn.captured.pop() file, lineno, func = location assert "Module already imported" in str(warning.message) assert file == str(conftest) assert func == "<module>" # the above conftest.py assert lineno == 4
def test_parameterset_for_fail_at_collect(pytester: Pytester) -> None: pytester.makeini(""" [pytest] {}=fail_at_collect """.format(EMPTY_PARAMETERSET_OPTION)) config = pytester.parseconfig() from _pytest.mark import pytest_configure, get_empty_parameterset_mark pytest_configure(config) with pytest.raises( Collector.CollectError, match=r"Empty parameter set in 'pytest_configure' at line \d\d+", ): get_empty_parameterset_mark(config, ["a"], pytest_configure) p1 = pytester.makepyfile(""" import pytest @pytest.mark.parametrize("empty", []) def test(): pass """) result = pytester.runpytest(str(p1)) result.stdout.fnmatch_lines([ "collected 0 items / 1 error", "* ERROR collecting test_parameterset_for_fail_at_collect.py *", "Empty parameter set in 'test' at line 3", "*= 1 error in *", ]) assert result.ret == ExitCode.INTERRUPTED
def test_parameterset_for_parametrize_marks( pytester: Pytester, mark: Optional[str] ) -> None: if mark is not None: pytester.makeini( """ [pytest] {}={} """.format( EMPTY_PARAMETERSET_OPTION, mark ) ) config = pytester.parseconfig() from _pytest.mark import pytest_configure, get_empty_parameterset_mark pytest_configure(config) result_mark = get_empty_parameterset_mark(config, ["a"], all) if mark in (None, ""): # normalize to the requested name mark = "skip" assert result_mark.name == mark assert result_mark.kwargs["reason"].startswith("got empty parameter set ") if mark == "xfail": assert result_mark.kwargs.get("run") is False
def test_issue4445_preparse(self, pytester: Pytester, capwarn) -> None: """#4445: Make sure the warning points to a reasonable location See origin of _issue_warning_captured at: _pytest.config.__init__.py:910 """ pytester.makeconftest(""" import nothing """) pytester.parseconfig("--help") # with stacklevel=2 the warning should originate from config._preparse and is # thrown by an erroneous conftest.py assert len(capwarn.captured) == 1 warning, location = capwarn.captured.pop() file, _, func = location assert "could not load initial conftests" in str(warning.message) assert f"config{os.sep}__init__.py" in file assert func == "_preparse"
def test_preserve_keys_order(pytester: Pytester) -> None: """Ensure keys order is preserved when saving dicts (#9205).""" from _pytest.cacheprovider import Cache config = pytester.parseconfig() cache = Cache.for_config(config, _ispytest=True) cache.set("foo", {"z": 1, "b": 2, "a": 3, "d": 10}) read_back = cache.get("foo", None) assert list(read_back.items()) == [("z", 1), ("b", 2), ("a", 3), ("d", 10)]
def test_cachedir_tag(pytester: Pytester) -> None: """Ensure we automatically create CACHEDIR.TAG file in the pytest_cache directory (#4278).""" from _pytest.cacheprovider import Cache from _pytest.cacheprovider import CACHEDIR_TAG_CONTENT config = pytester.parseconfig() cache = Cache.for_config(config, _ispytest=True) cache.set("foo", "bar") cachedir_tag_path = cache._cachedir.joinpath("CACHEDIR.TAG") assert cachedir_tag_path.read_bytes() == CACHEDIR_TAG_CONTENT
def test_issue4445_import_plugin(self, pytester: Pytester, capwarn) -> None: """#4445: Make sure the warning points to a reasonable location""" pytester.makepyfile(some_plugin=""" import pytest pytest.skip("thing", allow_module_level=True) """) pytester.syspathinsert() pytester.parseconfig("-p", "some_plugin") # with stacklevel=2 the warning should originate from # config.PytestPluginManager.import_plugin is thrown by a skipped plugin assert len(capwarn.captured) == 1 warning, location = capwarn.captured.pop() file, _, func = location assert "skipped plugin 'some_plugin': thing" in str(warning.message) assert f"config{os.sep}__init__.py" in file assert func == "_warn_about_skipped_plugins"
def test_gitignore(pytester: Pytester) -> None: """Ensure we automatically create .gitignore file in the pytest_cache directory (#3286).""" from _pytest.cacheprovider import Cache config = pytester.parseconfig() cache = Cache.for_config(config, _ispytest=True) cache.set("foo", "bar") msg = "# Created by pytest automatically.\n*\n" gitignore_path = cache._cachedir.joinpath(".gitignore") assert gitignore_path.read_text(encoding="UTF-8") == msg # Does not overwrite existing/custom one. gitignore_path.write_text("custom") cache.set("something", "else") assert gitignore_path.read_text(encoding="UTF-8") == "custom"
def test_does_not_create_boilerplate_in_existing_dirs( pytester: Pytester) -> None: from _pytest.cacheprovider import Cache pytester.makeini(""" [pytest] cache_dir = . """) config = pytester.parseconfig() cache = Cache.for_config(config, _ispytest=True) cache.set("foo", "bar") assert os.path.isdir("v") # cache contents assert not os.path.exists(".gitignore") assert not os.path.exists("README.md")
def test_hook_proxy(self, pytester: Pytester) -> None: """Test the gethookproxy function(#2016)""" config = pytester.parseconfig() session = Session.from_config(config) pytester.makepyfile(**{"tests/conftest.py": "", "tests/subdir/conftest.py": ""}) conftest1 = pytester.path.joinpath("tests/conftest.py") conftest2 = pytester.path.joinpath("tests/subdir/conftest.py") config.pluginmanager._importconftest(conftest1, importmode="prepend") ihook_a = session.gethookproxy(pytester.path / "tests") assert ihook_a is not None config.pluginmanager._importconftest(conftest2, importmode="prepend") ihook_b = session.gethookproxy(pytester.path / "tests") assert ihook_a is not ihook_b
def test_configure(self, pytester: Pytester) -> None: config = pytester.parseconfig() values = [] class A: def pytest_configure(self): values.append(self) config.pluginmanager.register(A()) assert len(values) == 0 config._do_configure() assert len(values) == 1 config.pluginmanager.register(A()) # leads to a configured() plugin assert len(values) == 2 assert values[0] != values[1] config._ensure_unconfigure() config.pluginmanager.register(A()) assert len(values) == 2
def test_parseconfig(pytester: Pytester) -> None: config1 = pytester.parseconfig() config2 = pytester.parseconfig() assert config2 is not config1
def test_external_plugins_integrated(pytester: Pytester, plugin) -> None: pytester.syspathinsert() pytester.makepyfile(**{plugin: ""}) with pytest.warns(pytest.PytestConfigWarning): pytester.parseconfig("-p", plugin)
def test_plugin_already_exists(pytester: Pytester) -> None: config = pytester.parseconfig("-p", "terminal") assert config.option.plugins == ["terminal"] config._do_configure() config._ensure_unconfigure()
def test_plugin_specify(pytester: Pytester) -> None: with pytest.raises(ImportError): pytester.parseconfig("-p", "nqweotexistent")