Python pytest.register_assert_rewrite() Examples

The following are 6 code examples of pytest.register_assert_rewrite(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pytest , or try the search function .
Example #1
Source File: test_warnings.py    From pytest with MIT License 6 votes vote down vote up
def test_issue4445_rewrite(self, testdir, capwarn):
        """#4445: Make sure the warning points to a reasonable location
        See origin of _issue_warning_captured at: _pytest.assertion.rewrite.py:241
        """
        testdir.makepyfile(some_mod="")
        conftest = testdir.makeconftest(
            """
                import some_mod
                import pytest

                pytest.register_assert_rewrite("some_mod")
            """
        )
        testdir.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 
Example #2
Source File: conftest.py    From PynamoDB with MIT License 5 votes vote down vote up
def assert_mypy_output(pytestconfig):
    pytest.importorskip('mypy')  # we only install mypy in python>=3.6 tests
    pytest.register_assert_rewrite('tests.mypy_helpers')

    from tests.mypy_helpers import assert_mypy_output
    return lambda program: assert_mypy_output(program, use_pdb=pytestconfig.getoption('usepdb')) 
Example #3
Source File: acceptance_test.py    From pytest with MIT License 5 votes vote down vote up
def test_pdb_can_be_rewritten(testdir):
    testdir.makepyfile(
        **{
            "conftest.py": """
                import pytest
                pytest.register_assert_rewrite("pdb")
                """,
            "__init__.py": "",
            "pdb.py": """
                def check():
                    assert 1 == 2
                """,
            "test_pdb.py": """
                def test():
                    import pdb
                    assert pdb.check()
                """,
        }
    )
    # Disable debugging plugin itself to avoid:
    # > INTERNALERROR> AttributeError: module 'pdb' has no attribute 'set_trace'
    result = testdir.runpytest_subprocess("-p", "no:debugging", "-vv")
    result.stdout.fnmatch_lines(
        [
            "    def check():",
            ">       assert 1 == 2",
            "E       assert 1 == 2",
            "E         +1",
            "E         -2",
            "",
            "pdb.py:2: AssertionError",
            "*= 1 failed in *",
        ]
    )
    assert result.ret == 1 
Example #4
Source File: test_assertion.py    From pytest with MIT License 5 votes vote down vote up
def test_register_assert_rewrite_checks_types(self) -> None:
        with pytest.raises(TypeError):
            pytest.register_assert_rewrite(["pytest_tests_internal_non_existing"])  # type: ignore
        pytest.register_assert_rewrite(
            "pytest_tests_internal_non_existing", "pytest_tests_internal_non_existing2"
        ) 
Example #5
Source File: test_assertrewrite.py    From pytest with MIT License 5 votes vote down vote up
def test_rewrite_warning(self, testdir):
        testdir.makeconftest(
            """
            import pytest
            pytest.register_assert_rewrite("_pytest")
        """
        )
        # needs to be a subprocess because pytester explicitly disables this warning
        result = testdir.runpytest_subprocess()
        result.stdout.fnmatch_lines(["*Module already imported*: _pytest"]) 
Example #6
Source File: test_assertion.py    From pytest with MIT License 4 votes vote down vote up
def test_rewrite_ast(self, testdir):
        testdir.tmpdir.join("pkg").ensure(dir=1)
        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
                @pytest.fixture
                def tool():
                    return pkg.helper.tool
            """,
            "pkg/other.py": """
                values = [3, 2]
                def tool():
                    assert values.pop() == 3
            """,
            "conftest.py": """
                pytest_plugins = ['pkg.plugin']
            """,
            "test_pkg.py": """
                import pkg.other
                def test_tool(tool):
                    tool()
                def test_other():
                    pkg.other.tool()
            """,
        }
        testdir.makepyfile(**contents)
        result = testdir.runpytest_subprocess("--assert=rewrite")
        result.stdout.fnmatch_lines(
            [
                ">*assert a == b*",
                "E*assert 2 == 3*",
                ">*assert values.pop() == 3*",
                "E*AssertionError",
            ]
        )