예제 #1
0
 def test_earlyinit(self, pytester: Pytester) -> None:
     p = pytester.makepyfile("""
         import pytest
         assert hasattr(pytest, 'mark')
     """)
     result = pytester.runpython(p)
     assert result.ret == 0
예제 #2
0
def test_conftest_global_import(pytester: Pytester) -> None:
    pytester.makeconftest("x=3")
    p = pytester.makepyfile(
        """
        from pathlib import Path
        import pytest
        from _pytest.config import PytestPluginManager
        conf = PytestPluginManager()
        mod = conf._importconftest(Path("conftest.py"), importmode="prepend")
        assert mod.x == 3
        import conftest
        assert conftest is mod, (conftest, mod)
        sub = Path("sub")
        sub.mkdir()
        subconf = sub / "conftest.py"
        subconf.write_text("y=4")
        mod2 = conf._importconftest(subconf, importmode="prepend")
        assert mod != mod2
        assert mod2.y == 4
        import conftest
        assert conftest is mod2, (conftest, mod)
    """
    )
    res = pytester.runpython(p)
    assert res.ret == 0
예제 #3
0
def test_unittest_expected_failure_for_passing_test_is_fail(
    pytester: Pytester,
    runner: str,
) -> None:
    script = pytester.makepyfile("""
        import unittest
        class MyTestCase(unittest.TestCase):
            @unittest.expectedFailure
            def test_passing_test_is_fail(self):
                assert True
        if __name__ == '__main__':
            unittest.main()
    """)

    if runner == "pytest":
        result = pytester.runpytest("-rxX")
        result.stdout.fnmatch_lines([
            "*MyTestCase*test_passing_test_is_fail*",
            "Unexpected success",
            "*1 failed*",
        ])
    else:
        result = pytester.runpython(script)
        result.stderr.fnmatch_lines(
            ["*1 test in*", "*(unexpected successes=1)*"])

    assert result.ret == 1
예제 #4
0
 def test_import_star_pytest(self, pytester: Pytester) -> None:
     p = pytester.makepyfile("""
         from pytest import *
         #Item
         #File
         main
         skip
         xfail
     """)
     result = pytester.runpython(p)
     assert result.ret == 0
예제 #5
0
 def test_double_pytestcmdline(self, pytester: Pytester) -> None:
     p = pytester.makepyfile(run="""
         import pytest
         pytest.main()
         pytest.main()
     """)
     pytester.makepyfile("""
         def test_hello():
             pass
     """)
     result = pytester.runpython(p)
     result.stdout.fnmatch_lines(["*1 passed*", "*1 passed*"])
예제 #6
0
 def test_import_star_py_dot_test(self, pytester: Pytester) -> None:
     p = pytester.makepyfile("""
         from py.test import *
         #collect
         #cmdline
         #Item
         # assert collect.Item is Item
         # assert collect.Collector is Collector
         main
         skip
         xfail
     """)
     result = pytester.runpython(p)
     assert result.ret == 0
예제 #7
0
def test_unittest_expected_failure_for_failing_test_is_xfail(
        pytester: Pytester, runner) -> None:
    script = pytester.makepyfile("""
        import unittest
        class MyTestCase(unittest.TestCase):
            @unittest.expectedFailure
            def test_failing_test_is_xfail(self):
                assert False
        if __name__ == '__main__':
            unittest.main()
    """)
    if runner == "pytest":
        result = pytester.runpytest("-rxX")
        result.stdout.fnmatch_lines(
            ["*XFAIL*MyTestCase*test_failing_test_is_xfail*", "*1 xfailed*"])
    else:
        result = pytester.runpython(script)
        result.stderr.fnmatch_lines(
            ["*1 test in*", "*OK*(expected failures=1)*"])
    assert result.ret == 0
예제 #8
0
def test_conftest_global_import(pytester: Pytester) -> None:
    pytester.makeconftest("x=3")
    p = pytester.makepyfile("""
        import py, pytest
        from _pytest.config import PytestPluginManager
        conf = PytestPluginManager()
        mod = conf._importconftest(py.path.local("conftest.py"), importmode="prepend")
        assert mod.x == 3
        import conftest
        assert conftest is mod, (conftest, mod)
        subconf = py.path.local().ensure("sub", "conftest.py")
        subconf.write("y=4")
        mod2 = conf._importconftest(subconf, importmode="prepend")
        assert mod != mod2
        assert mod2.y == 4
        import conftest
        assert conftest is mod2, (conftest, mod)
    """)
    res = pytester.runpython(p)
    assert res.ret == 0
예제 #9
0
def test_zipimport_hook(pytester: Pytester) -> None:
    """Test package loader is being used correctly (see #1837)."""
    zipapp = pytest.importorskip("zipapp")
    pytester.path.joinpath("app").mkdir()
    pytester.makepyfile(
        **{
            "app/foo.py": """
            import pytest
            def main():
                pytest.main(['--pyargs', 'foo'])
        """
        }
    )
    target = pytester.path.joinpath("foo.zip")
    zipapp.create_archive(
        str(pytester.path.joinpath("app")), str(target), main="foo:main"
    )
    result = pytester.runpython(target)
    assert result.ret == 0
    result.stderr.fnmatch_lines(["*not found*foo*"])
    result.stdout.no_fnmatch_line("*INTERNALERROR>*")