def test_run__file_not_in_test_dir__simply_skipped(self): code_snippet = "def test_not_good():\n pass" tree = get_tree_from_str(code_snippet) args = SysArgs( test_func_name_validator_module=None, test_func_name_validator_regex="test_funkyconvention", ) # Verify that it would be identified # as an invalid test if in tests dir first checker = MyFlake8Plugin(tree, "/tmp/tests/test_garbage.py") checker.parse_options(None, args, None) expected = [( 1, 0, "TN101 test function name does not match the convention (test_not_good)", MyFlake8Plugin, )] res = list(checker.run()) assert res == expected # Simulate a non-test-file with test functions # and confirm that it is simply skipped checker = MyFlake8Plugin(tree, "/tmp/regular_dir/garbage.py") checker.parse_options(None, args, None) res = list(checker.run()) assert not res
def test_get_invalid_test_methods__simple_case__finds_invalid_match(self): code_snippet = "import garbage\n\ndef test_im_an_valid_method():\n pass" tree = get_tree_from_str(code_snippet) def validator(func_name): return "test_no_match" in func_name invalid_methods = list( MyFlake8Plugin.get_invalid_test_methods(tree, validator)) assert invalid_methods
def test_run__using_regex_on_sample_file__no_invalid_match(self): tree = get_tree(SAMPLE_FILE_PATH) args = SysArgs( test_func_name_validator_regex="test_.*", test_func_name_validator_module=None, ) checker = MyFlake8Plugin(tree, SAMPLE_FILE_PATH) checker.parse_options(None, args, None) expected = [] res = list(checker.run()) assert res == expected
def test_run__using_regex__no_invalid_match(self): code_snippet = "import garbage\n\ndef foo():\n pass\n\ndef bar():\n pass" tree = get_tree_from_str(code_snippet) args = SysArgs( test_func_name_validator_regex="test_funkyconvention.*", test_func_name_validator_module=None, ) checker = MyFlake8Plugin(tree, SAMPLE_FILE_PATH) checker.parse_options(None, args, None) expected = [] res = list(checker.run()) assert res == expected
def test_run__using_regex_on_sample_file__finds_invalid_match(self): tree = get_tree(SAMPLE_FILE_PATH) args = SysArgs( test_func_name_validator_regex="test_funkyconvention.*", test_func_name_validator_module=None, ) checker = MyFlake8Plugin(tree, SAMPLE_FILE_PATH) checker.parse_options(None, args, None) expected = [ ( 10, 0, "TN101 test function name does not match the convention (test_invalid_module_sample)", MyFlake8Plugin, ), ( 22, 4, "TN101 test function name does not match the convention (test_invalid_method_sample)", MyFlake8Plugin, ), ( 25, 4, "TN101 test function name does not match the convention (test_test_funkyconvention_method_is_valid)", MyFlake8Plugin, ), ( 33, 4, "TN101 test function name does not match the convention (test_invalid_unittest_method_sample)", MyFlake8Plugin, ), ] res = list(checker.run()) assert res == expected
def test_report__should_pass(self): tree = get_tree(SAMPLE_FILE_PATH) checker = MyFlake8Plugin(tree, SAMPLE_FILE_PATH) assert checker.report("funky_message") is None
def test_get_test_func_name_validator__no_validator__raises(self): tree = get_tree(SAMPLE_FILE_PATH) checker = MyFlake8Plugin(tree, SAMPLE_FILE_PATH) checker.parse_options(None, SysArgs(None, None), None) with pytest.raises(PluginTestNameConfigurationError): checker.get_test_func_name_validator()
def test_format_code__should_pass(self): checker = MyFlake8Plugin(None, SAMPLE_FILE_PATH) assert checker.format_code(302) == "TN302"