@@ -1372,6 +1372,42 @@ def test_resolve_pkg_root_and_module_name_ns_multiple_levels(
13721372 )
13731373 assert mod is mod2
13741374
1375+ def test_ns_multiple_levels_import_rewrite_assertions (
1376+ self ,
1377+ tmp_path : Path ,
1378+ monkeypatch : MonkeyPatch ,
1379+ pytester : Pytester ,
1380+ ) -> None :
1381+ """Check assert rewriting with `--import-mode=importlib` (#12659)."""
1382+ self .setup_directories (tmp_path , monkeypatch , pytester )
1383+ code = dedent ("""
1384+ def test():
1385+ assert "four lights" == "five lights"
1386+ """ )
1387+
1388+ # A case is in a subdirectory with an `__init__.py` file.
1389+ test_py = tmp_path / "src/dist2/com/company/calc/algo/test_demo.py"
1390+ test_py .write_text (code , encoding = "UTF-8" )
1391+
1392+ pkg_root , module_name = resolve_pkg_root_and_module_name (
1393+ test_py , consider_namespace_packages = True
1394+ )
1395+ assert (pkg_root , module_name ) == (
1396+ tmp_path / "src/dist2" ,
1397+ "com.company.calc.algo.test_demo" ,
1398+ )
1399+
1400+ result = pytester .runpytest ("--import-mode=importlib" , test_py )
1401+
1402+ result .stdout .fnmatch_lines (
1403+ [
1404+ "E AssertionError: assert 'four lights' == 'five lights'" ,
1405+ "E *" ,
1406+ "E - five lights*" ,
1407+ "E + four lights" ,
1408+ ]
1409+ )
1410+
13751411 @pytest .mark .parametrize ("import_mode" , ["prepend" , "append" , "importlib" ])
13761412 def test_incorrect_namespace_package (
13771413 self ,
0 commit comments