def test_number_and_allow_unicode(self, pytester: Pytester): pytester.maketxtfile( test_doc=""" >>> from collections import namedtuple >>> T = namedtuple('T', 'a b c') >>> T(a=0.2330000001, b=u'str', c=b'bytes') # doctest: +ALLOW_UNICODE, +ALLOW_BYTES, +NUMBER T(a=0.233, b=u'str', c='bytes') """ ) reprec = pytester.inline_run() reprec.assertoutcome(passed=1)
def test_issue197_parametrize_emptyset(self, pytester: Pytester) -> None: pytester.makepyfile( """ import pytest @pytest.mark.parametrize('arg', []) def test_function(arg): pass """ ) reprec = pytester.inline_run() reprec.assertoutcome(skipped=1)
def test_trial_testfunction_skip_property(self, pytester: Pytester) -> None: testpath = pytester.makepyfile(""" from twisted.trial import unittest class MyTestCase(unittest.TestCase): def test_func(self): pass test_func.skip = 'dont run' """) reprec = pytester.inline_run(testpath, "-s") reprec.assertoutcome(skipped=1)
def test_nottest_function_decorator(pytester: Pytester) -> None: pytester.makepyfile(""" import nose.tools @nose.tools.nottest def test_prefix(): pass """) reprec = pytester.inline_run() assert not reprec.getfailedcollections() calls = reprec.getreports("pytest_runtest_logreport") assert not calls
def test_parsefactories_relative_node_ids( self, pytester: Pytester, chdir: str, testarg: str, expect_ntests_passed: int ) -> None: """#616""" dirs = self._setup_tree(pytester) print("pytest run in cwd: %s" % (dirs[chdir].relative_to(pytester.path))) print("pytestarg : %s" % (testarg)) print("expected pass : %s" % (expect_ntests_passed)) os.chdir(dirs[chdir]) reprec = pytester.inline_run(testarg, "-q", "--traceconfig") reprec.assertoutcome(passed=expect_ntests_passed)
def test_module_level_pytestmark(pytester: Pytester) -> None: testpath = pytester.makepyfile(""" import unittest import pytest pytestmark = pytest.mark.xfail class MyTestCase(unittest.TestCase): def test_func1(self): assert 0 """) reprec = pytester.inline_run(testpath, "-s") reprec.assertoutcome(skipped=1)
def test_unicode_string(self, pytester: Pytester): """Test that doctests which output unicode fail in Python 2 when the ALLOW_UNICODE option is not used. The same test should pass in Python 3. """ pytester.maketxtfile(test_doc=""" >>> b'12'.decode('ascii') '12' """) reprec = pytester.inline_run() reprec.assertoutcome(passed=1)
def test_skips_on_false_string(self, pytester: Pytester) -> None: pytester.makepyfile( """ import pytest @pytest.mark.skip('False') def test_foo(): pass """ ) rec = pytester.inline_run() rec.assertoutcome(skipped=1)
def test_keyword_extra_dash(self, pytester: Pytester) -> None: p = pytester.makepyfile(""" def test_one(): assert 0 test_one.mykeyword = True """) # with argparse the argument to an option cannot # start with '-' reprec = pytester.inline_run("-k", "-mykeyword", p) passed, skipped, failed = reprec.countoutcomes() assert passed + skipped + failed == 0
def test_doctestmodule(self, pytester: Pytester): p = pytester.makepyfile(""" ''' >>> x = 1 >>> x == 1 False ''' """) reprec = pytester.inline_run(p, "--doctest-modules") reprec.assertoutcome(failed=1)
def test_parametrize_with_module(pytester: Pytester) -> None: pytester.makepyfile(""" import pytest @pytest.mark.parametrize("arg", [pytest,]) def test_func(arg): pass """) rec = pytester.inline_run() passed, skipped, fail = rec.listoutcomes() expected_id = "test_func[" + pytest.__name__ + "]" assert passed[0].nodeid.split("::")[-1] == expected_id
def test_SkipTest_in_test(pytester: Pytester) -> None: pytester.makepyfile( """ import nose def test_skipping(): raise nose.SkipTest("in test") """ ) reprec = pytester.inline_run() reprec.assertoutcome(skipped=1)
def test_longreprtext_collect_skip(self, pytester: Pytester) -> None: """CollectReport.longreprtext can handle non-str ``longrepr`` attributes (#7559)""" pytester.makepyfile(""" import pytest pytest.skip(allow_module_level=True) """) rec = pytester.inline_run() calls = rec.getcalls("pytest_collectreport") _, call = calls assert isinstance(call.report.longrepr, tuple) assert "Skipped" in call.report.longreprtext
def test_tmp_path_fallback_uid_not_found(pytester: Pytester) -> None: """Test that tmp_path works even if the current process's user id does not correspond to a valid user. """ pytester.makepyfile(""" def test_some(tmp_path): assert tmp_path.is_dir() """) reprec = pytester.inline_run() reprec.assertoutcome(passed=1)
def test_inline_run_taking_and_restoring_a_sys_modules_snapshot( self, pytester: Pytester, monkeypatch: MonkeyPatch ) -> None: spy_factory = self.spy_factory() monkeypatch.setattr(pytester_mod, "SysModulesSnapshot", spy_factory) pytester.syspathinsert() original = dict(sys.modules) pytester.makepyfile(import1="# you son of a silly person") pytester.makepyfile(import2="# my hovercraft is full of eels") test_mod = pytester.makepyfile( """ import import1 def test_foo(): import import2""" ) pytester.inline_run(str(test_mod)) assert len(spy_factory.instances) == 1 spy = spy_factory.instances[0] assert spy._spy_restore_count == 1 assert sys.modules == original assert all(sys.modules[x] is original[x] for x in sys.modules)
def test_keyword_extra(self, pytester: Pytester) -> None: p = pytester.makepyfile( """ def test_one(): assert 0 test_one.mykeyword = True """ ) reprec = pytester.inline_run("-k", "mykeyword", p) passed, skipped, failed = reprec.countoutcomes() assert failed == 1
def test_tmpdir_too_long_on_parametrization(pytester: Pytester) -> None: pytester.makepyfile( """ import pytest @pytest.mark.parametrize("arg", ["1"*1000]) def test_some(arg, tmpdir): tmpdir.ensure("hello") """ ) reprec = pytester.inline_run() reprec.assertoutcome(passed=1)
def test_import_prepend_append(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> None: root1 = pytester.mkdir("root1") root2 = pytester.mkdir("root2") root1.joinpath("x456.py").touch() root2.joinpath("x456.py").touch() p = root2.joinpath("test_x456.py") monkeypatch.syspath_prepend(str(root1)) p.write_text( textwrap.dedent("""\ import x456 def test(): assert x456.__file__.startswith({!r}) """.format(str(root2)))) with monkeypatch.context() as mp: mp.chdir(root2) reprec = pytester.inline_run("--import-mode=append") reprec.assertoutcome(passed=0, failed=1) reprec = pytester.inline_run() reprec.assertoutcome(passed=1)
def test_trial_testcase_runtest_not_collected(self, pytester: Pytester) -> None: pytester.makepyfile(""" from twisted.trial.unittest import TestCase class TC(TestCase): def test_hello(self): pass """) reprec = pytester.inline_run(*self.ignore_unclosed_socket_warning) reprec.assertoutcome(passed=1) pytester.makepyfile(""" from twisted.trial.unittest import TestCase class TC(TestCase): def runTest(self): pass """) reprec = pytester.inline_run(*self.ignore_unclosed_socket_warning) reprec.assertoutcome(passed=1)
def test_new_instances(pytester: Pytester) -> None: testpath = pytester.makepyfile(""" import unittest class MyTestCase(unittest.TestCase): def test_func1(self): self.x = 2 def test_func2(self): assert not hasattr(self, 'x') """) reprec = pytester.inline_run(testpath) reprec.assertoutcome(passed=2)
def test_single_tuple_unwraps_values(self, pytester: Pytester) -> None: pytester.makepyfile( """ import pytest @pytest.mark.parametrize(('arg',), [(1,)]) def test_function(arg): assert arg == 1 """ ) reprec = pytester.inline_run() reprec.assertoutcome(passed=1)
def test_tmp_path_factory(pytester: Pytester) -> None: pytester.makepyfile(""" import pytest @pytest.fixture(scope='session') def session_dir(tmp_path_factory): return tmp_path_factory.mktemp('data', numbered=False) def test_some(session_dir): assert session_dir.is_dir() """) reprec = pytester.inline_run() reprec.assertoutcome(passed=1)
def test_non_ignored_whitespace_glob(self, pytester: Pytester): pytester.makeini(""" [pytest] doctest_optionflags = ELLIPSIS """) p = pytester.maketxtfile(xdoc=""" >>> a = "foo " >>> print(a) foo """) reprec = pytester.inline_run(p, "--doctest-glob=x*.txt") reprec.assertoutcome(failed=1, passed=0)
def test_tmp_path_always_is_realpath(pytester: Pytester, monkeypatch) -> None: # for reasoning see: test_tmpdir_always_is_realpath test-case realtemp = pytester.mkdir("myrealtemp") linktemp = pytester.path.joinpath("symlinktemp") attempt_symlink_to(linktemp, str(realtemp)) monkeypatch.setenv("PYTEST_DEBUG_TEMPROOT", str(linktemp)) pytester.makepyfile(""" def test_1(tmp_path): assert tmp_path.resolve() == tmp_path """) reprec = pytester.inline_run() reprec.assertoutcome(passed=1)
def test_tmp_path_fallback_tox_env(pytester: Pytester, monkeypatch) -> None: """Test that tmp_path works even if environment variables required by getpass module are missing (#1010). """ monkeypatch.delenv("USER", raising=False) monkeypatch.delenv("USERNAME", raising=False) pytester.makepyfile(""" def test_some(tmp_path): assert tmp_path.is_dir() """) reprec = pytester.inline_run() reprec.assertoutcome(passed=1)
def test_unittest_mock(self, pytester: Pytester) -> None: pytester.makepyfile(""" import unittest.mock class T(unittest.TestCase): @unittest.mock.patch("os.path.abspath") def test_hello(self, abspath): import os os.path.abspath("hello") abspath.assert_any_call("hello") """) reprec = pytester.inline_run() reprec.assertoutcome(passed=1)
def test_recwarn_functional(pytester: Pytester) -> None: pytester.makepyfile( """ import warnings def test_method(recwarn): warnings.warn("hello") warn = recwarn.pop() assert isinstance(warn.message, UserWarning) """ ) reprec = pytester.inline_run() reprec.assertoutcome(passed=1)
def test_setup_fails_again_on_all_tests(pytester: Pytester) -> None: p = pytester.makepyfile(""" import pytest def setup_module(mod): raise ValueError(42) def test_function1(): pass def test_function2(): pass """) reprec = pytester.inline_run(p) reprec.assertoutcome(failed=2)
def test_setup_that_skips_calledagain(pytester: Pytester) -> None: p = pytester.makepyfile(""" import pytest def setup_module(mod): pytest.skip("x") def test_function1(): pass def test_function2(): pass """) reprec = pytester.inline_run(p) reprec.assertoutcome(skipped=2)
def test_module_with_global_test(self, pytester: Pytester) -> None: pytester.makepyfile( """ __test__ = False def test_hello(): pass """ ) reprec = pytester.inline_run() assert not reprec.getfailedcollections() calls = reprec.getreports("pytest_runtest_logreport") assert not calls