Repository: pytest-dev/pytest
File: testing/test_pathlib.py (Python)

Code before:
        )

        mod = import_path(
            algorithms_py,
            mode=import_mode,
            root=tmp_path,
            consider_namespace_packages=True,
        )
        assert mod.__name__ == "com.company.calc.algo.algorithms"
        assert mod.__file__ == str(algorithms_py)

        # Ensure we do not import the same module again (#11475).
        mod2 = import_path(
            algorithms_py,
            mode=import_mode,
            root=tmp_path,
            consider_namespace_packages=True,
        )
        assert mod is mod2

    def test_ns_multiple_levels_import_rewrite_assertions(
        self,
        tmp_path: Path,
        monkeypatch: MonkeyPatch,
        pytester: Pytester,
    ) -> None:
        models_py, algorithms_py = self.setup_directories(
            tmp_path, monkeypatch, pytester
        )
        code = dedent("""
        def test():
            assert "four lights" == "five lights"
        """)

        # A case is in a subdirectory with an `__init__.py` file.
        test_py = tmp_path / tmp_path / "src/dist2/com/company/calc/algo/test_demo.py"
        test_py.write_text(code, encoding="UTF-8")

        pkg_root, module_name = resolve_pkg_root_and_module_name(
            test_py, consider_namespace_packages=True
        )
        assert (pkg_root, module_name) == (
            tmp_path / "src/dist2",
            "com.company.calc.algo.test_demo",
        )

        result = pytester.runpytest("--import-mode=importlib", test_py)

        result.stdout.fnmatch_lines(
            [
                "E       AssertionError: assert 'four lights' == 'five lights'",

Review comment (suggestion):
```suggestion
    ) -> None:
    """Check assert rewriting with `--import-mode=importlib` (#12659)."""
```

Code after:
        )

        mod = import_path(
            algorithms_py,
            mode=import_mode,
            root=tmp_path,
            consider_namespace_packages=True,
        )
        assert mod.__name__ == "com.company.calc.algo.algorithms"
        assert mod.__file__ == str(algorithms_py)

        # Ensure we do not import the same module again (#11475).
        mod2 = import_path(
            algorithms_py,
            mode=import_mode,
            root=tmp_path,
            consider_namespace_packages=True,
        )
        assert mod is mod2

    def test_ns_multiple_levels_import_rewrite_assertions(
        self,
        tmp_path: Path,
        monkeypatch: MonkeyPatch,
        pytester: Pytester,
    ) -> None:
        """Check assert rewriting with `--import-mode=importlib` (#12659)."""
        self.setup_directories(tmp_path, monkeypatch, pytester)
        code = dedent("""
        def test():
            assert "four lights" == "five lights"
        """)

        # A case is in a subdirectory with an `__init__.py` file.
        test_py = tmp_path / "src/dist2/com/company/calc/algo/test_demo.py"
        test_py.write_text(code, encoding="UTF-8")

        pkg_root, module_name = resolve_pkg_root_and_module_name(
            test_py, consider_namespace_packages=True
        )
        assert (pkg_root, module_name) == (
            tmp_path / "src/dist2",
            "com.company.calc.algo.test_demo",
        )

        result = pytester.runpytest("--import-mode=importlib", test_py)

        result.stdout.fnmatch_lines(
            [
                "E       AssertionError: assert 'four lights' == 'five lights'",
                "E         *",