def test_no_command(self) -> None: argv = ["-m", "some_module"] stdout, stderr = io.StringIO(), io.StringIO() main.run(argv=argv, stdout=stdout, stderr=stderr) self.assertEqual("", stdout.getvalue()) self.assertEqual( """\ usage: pyicontract-hypothesis [-h] {test,ghostwrite} ... pyicontract-hypothesis: error: argument command: invalid choice: 'some_module' (choose from 'test', 'ghostwrite') """, stderr.getvalue(), )
def test_well_formatted_with_two_arguments(self) -> None: # This test is related to the issue: # https://github.com/mristin/icontract-hypothesis/issues/29 # fmt: off argv = [ "ghostwrite", "--module", "test_samples.pyicontract_hypothesis.well_formatted_with_two_arguments", "--explicit" ] # fmt: on stdout = io.StringIO() stderr = io.StringIO() exit_code = main.run(argv=argv, stdout=stdout, stderr=stderr) self.assertEqual("", stderr.getvalue()) self.assertEqual(exit_code, 0) this_dir = pathlib.Path(os.path.realpath(__file__)).parent expected_pth = ( this_dir.parent.parent / "test_samples/pyicontract_hypothesis/expected_ghostwrites" / ( "for_{}.py".format( TestGhostwrite.test_well_formatted_with_two_arguments.__name__ ) ) ) expected = expected_pth.read_text() self.assertEqual(expected, stdout.getvalue())
def test_non_bare_and_non_explicit(self) -> None: # fmt: off argv = [ "ghostwrite", "--module", "test_samples.pyicontract_hypothesis.sample_module", ] # fmt: on stdout = io.StringIO() stderr = io.StringIO() exit_code = main.run(argv=argv, stdout=stdout, stderr=stderr) self.assertEqual("", stderr.getvalue()) self.assertEqual(exit_code, 0) this_dir = pathlib.Path(os.path.realpath(__file__)).parent expected_pth = ( this_dir.parent.parent / "test_samples/pyicontract_hypothesis/expected_ghostwrites" / ( "for_{}.py".format( TestGhostwrite.test_non_bare_and_non_explicit.__name__ ) ) ) expected = expected_pth.read_text() self.assertEqual(expected, stdout.getvalue())
def test_common_case(self) -> None: this_dir = pathlib.Path(os.path.realpath(__file__)).parent pth = (this_dir.parent.parent / "test_samples/pyicontract_hypothesis/sample_module.py") argv = ["test", "--inspect", "--path", str(pth)] stdout = io.StringIO() stderr = io.StringIO() # This is merely a smoke test. exit_code = main.run(argv=argv, stdout=stdout, stderr=stderr) self.assertEqual("", stderr.getvalue()) self.assertEqual(exit_code, 0) self.assertEqual( textwrap.dedent("""\ some_func at line 10: hypothesis.given( fixed_dictionaries({'x': integers(min_value=1)}) ) another_func at line 19: hypothesis.given( fixed_dictionaries({'x': integers(min_value=1).filter(lambda x: square_greater_than_zero(x))}) ) yet_another_func at line 28: hypothesis.given( fixed_dictionaries({'x': integers(), 'y': integers()}).filter(lambda d: d['x'] < d['y']) ) """), stdout.getvalue(), )
def test_with_include_exclude(self) -> None: this_dir = pathlib.Path(os.path.realpath(__file__)).parent pth = (this_dir.parent.parent / "test_samples/pyicontract_hypothesis/sample_module.py") # fmt: off argv = [ "test", "--path", str(pth), "--include", ".*_func", "--exclude", "some.*", ] # fmt: on stdout = io.StringIO() stderr = io.StringIO() # This is merely a smoke test. exit_code = main.run(argv=argv, stdout=stdout, stderr=stderr) self.assertEqual("", stderr.getvalue()) self.assertEqual(exit_code, 0)
def test_common_case(self) -> None: this_dir = pathlib.Path(os.path.realpath(__file__)).parent pth = (this_dir.parent.parent / "test_samples/pyicontract_hypothesis/sample_module.py") argv = ["test", "--path", str(pth)] stdout = io.StringIO() stderr = io.StringIO() # This is merely a smoke test. exit_code = main.run(argv=argv, stdout=stdout, stderr=stderr) self.assertEqual("", stderr.getvalue()) self.assertEqual(exit_code, 0) out = re.sub("\(time delta [^)]*\)", "(time delta <erased>)", stdout.getvalue()) self.assertEqual( textwrap.dedent("""\ Tested some_func at line 10 (time delta <erased>). Tested another_func at line 19 (time delta <erased>). Tested yet_another_func at line 28 (time delta <erased>). """), out, )
def test_with_settings(self) -> None: this_dir = pathlib.Path(os.path.realpath(__file__)).parent pth = (this_dir.parent.parent / "test_samples/pyicontract_hypothesis/sample_module.py") # fmt: off argv = [ "test", "--inspect", "--path", str(pth), "--settings", "max_examples=5", 'suppress_health_check=["too_slow"]' ] # fmt: on stdout = io.StringIO() stderr = io.StringIO() # This is merely a smoke test. exit_code = main.run(argv=argv, stdout=stdout, stderr=stderr) self.assertEqual("", stderr.getvalue()) self.assertEqual(exit_code, 0) self.assertEqual( textwrap.dedent("""\ some_func at line 10: hypothesis.given( fixed_dictionaries({'x': integers(min_value=1)}) ) hypothesis.settings( max_examples=5, suppress_health_check=[HealthCheck.too_slow] ) another_func at line 19: hypothesis.given( fixed_dictionaries({'x': integers(min_value=1).filter(lambda x: square_greater_than_zero(x))}) ) hypothesis.settings( max_examples=5, suppress_health_check=[HealthCheck.too_slow] ) yet_another_func at line 28: hypothesis.given( fixed_dictionaries({'x': integers(), 'y': integers()}).filter(lambda d: d['x'] < d['y']) ) hypothesis.settings( max_examples=5, suppress_health_check=[HealthCheck.too_slow] ) """), stdout.getvalue(), )
def test_nonexisting_file(self) -> None: path = "doesnt-exist.{}".format(uuid.uuid4()) argv = ["test", "--path", path] stdout = io.StringIO() stderr = io.StringIO() exit_code = main.run(argv=argv, stdout=stdout, stderr=stderr) self.assertEqual( "The file to be tested does not exist: {}".format(path), stderr.getvalue().strip(), ) self.assertEqual(exit_code, 1)
def test_with_settings(self) -> None: this_dir = pathlib.Path(os.path.realpath(__file__)).parent pth = (this_dir.parent.parent / "test_samples/pyicontract_hypothesis/sample_module.py") argv = ["test", "--path", str(pth), "--settings", "max_examples=5"] stdout = io.StringIO() stderr = io.StringIO() # This is merely a smoke test. exit_code = main.run(argv=argv, stdout=stdout, stderr=stderr) self.assertEqual("", stderr.getvalue()) self.assertEqual(exit_code, 0)
def test_path(self) -> None: this_dir = pathlib.Path(os.path.realpath(__file__)).parent path = ( this_dir.parent.parent / "test_samples/pyicontract_hypothesis/sample_module.py" ) # fmt: off argv = [ "ghostwrite", "--path", str(path) ] # fmt: on stdout = io.StringIO() stderr = io.StringIO() exit_code = main.run(argv=argv, stdout=stdout, stderr=stderr) self.assertEqual("", stderr.getvalue()) self.assertEqual(exit_code, 0) # The qualified name of the module depends on the sys.path which, in turn, is modified # by the test executor (*e.g.*, IDE can add temporarily directories to sys.path # automatically when executing tests). # # Since the qualified name of a module depends on ``sys.path``, we need to replace it with # a generic placeholder to make the test output uniform and reproducible. qualified_name = _ghostwrite._qualified_module_name_from_path( path=path, sys_path=sys.path ) got = stdout.getvalue() got = got.replace(qualified_name, "placeholder_for_sample_module") expected_pth = ( this_dir.parent.parent / "test_samples/pyicontract_hypothesis/expected_ghostwrites" / ("for_{}.txt".format(TestGhostwrite.test_path.__name__)) ) expected = expected_pth.read_text() self.assertEqual(expected, got)