def test_reload_is_same_and_reloads(self, pytester: Pytester) -> None:
        """Reloading a (collected) module after change picks up the change."""
        pytester.makeini(
            """
            [pytest]
            python_files = *.py
            """
        )
        pytester.makepyfile(
            file="""
            def reloaded():
                return False

            def rewrite_self():
                with open(__file__, 'w', encoding='utf-8') as self:
                    self.write('def reloaded(): return True')
            """,
            test_fun="""
            import sys
            from importlib import reload

            def test_loader():
                import file
                assert not file.reloaded()
                file.rewrite_self()
                assert sys.modules["file"] is reload(file)
                assert file.reloaded()
            """,
        )
        result = pytester.runpytest()
        result.stdout.fnmatch_lines(["* 1 passed*"])
Reloading a (collected) module after change picks up the change.
testing/test_assertrewrite.py