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

Code before:
            class DummyDistInfo(object):
                version = '1.0'
                files = ('spamplugin.py', 'hampkg/__init__.py')
                entry_points = (DummyEntryPoint(),)
                metadata = {'name': 'foo'}

            def distributions():
                return (DummyDistInfo(),)

            importlib.metadata.distributions = distributions
            pytest.main()
            """,
            "test_foo.py": """\
            def test(check_first):
                check_first([10, 30], 30)

            def test2(check_first2):
                check_first2([10, 30], 30)
            """,
        }
        pytester.makepyfile(**contents)
        result = pytester.run(sys.executable, *args)
        if mode == "plain":
            expected = "E       AssertionError"
        elif mode == "rewrite":
            expected = "*assert 10 == 30*"
        else:
            assert 0

        if not disable_plugin_autoload or explicit_specify:
            result.assert_outcomes(failed=2)
            result.stdout.fnmatch_lines([expected, expected])
        else:
            result.assert_outcomes(errors=2)
            result.stdout.fnmatch_lines(
                [
                    "E       fixture 'check_first' not found",
                    "E       fixture 'check_first2' not found",
                ]
            )

    def test_rewrite_ast(self, pytester: Pytester) -> None:
        pytester.mkdir("pkg")
        contents = {
            "pkg/__init__.py": """
                import pytest
                pytest.register_assert_rewrite('pkg.helper')
            """,
            "pkg/helper.py": """
                def tool():
                    a, b = 2, 3

Review comment (refactor):
With all the additional logic in this test, I wonder if it would be better to have a fixture for the `hampkg` and split this into two distinct tests at least (autoload enabled/disabled)?

Code after:
                metadata = {'name': 'foo'}

            def distributions():
                return (DummyDistInfo(),)

            importlib.metadata.distributions = distributions
            pytest.main()
            """,
            "test_foo.py": """\
            def test(check_first):
                check_first([10, 30], 30)

            def test2(check_first2):
                check_first2([10, 30], 30)
            """,
        }
        pytester.makepyfile(**contents)
        result = pytester.run(sys.executable, *args)
        if mode == "plain":
            expected = "E       AssertionError"
        elif mode == "rewrite":
            expected = "*assert 10 == 30*"
        else:
            assert 0

        if not disable_plugin_autoload or explicit_specify:
            result.assert_outcomes(failed=2)
            result.stdout.fnmatch_lines([expected, expected])
        else:
            result.assert_outcomes(errors=2)
            result.stdout.fnmatch_lines(
                [
                    "E       fixture 'check_first' not found",
                    "E       fixture 'check_first2' not found",
                ]
            )

    def test_rewrite_ast(self, pytester: Pytester) -> None:
        pytester.mkdir("pkg")
        contents = {
            "pkg/__init__.py": """
                import pytest
                pytest.register_assert_rewrite('pkg.helper')
            """,
            "pkg/helper.py": """
                def tool():
                    a, b = 2, 3
                    assert a == b
            """,
            "pkg/plugin.py": """
                import pytest, pkg.helper