def test_execute_single_case(self): # Given test_yaml = textwrap.dedent(""" --- version: '1.0' config: host: test.domain cases: - name: "Basic" tests: - name: "Test root URL" url: "/" assertions: - name: status_code expected: 200 """) responses.add( responses.GET, 'http://test.domain/', status=200, ) test_data = yaml.safe_load(test_yaml) # When suite = self.loader.load_tests_from_yaml( test_data, '/path/to/foo.yaml') # Then self.assertIsInstance(suite, TestSuite) self.assertEqual(suite.countTestCases(), 1) case = next(find_test_cases(suite)) # Given result = ResultCollecter() # When case(result) # Then self.assertTrue(result.wasSuccessful())
def test_execute_single_case(self): # Given test_yaml = textwrap.dedent(""" --- version: '1.0' config: host: test.domain cases: - name: "Basic" tests: - name: "Test root URL" url: "/" assertions: - name: status_code expected: 200 """) responses.add( responses.GET, 'http://test.domain/', status=200, ) test_data = yaml.safe_load(test_yaml) # When suite = self.loader.load_tests_from_yaml(test_data, '/path/to/foo.yaml') # Then self.assertIsInstance(suite, TestSuite) self.assertEqual(suite.countTestCases(), 1) case = next(find_test_cases(suite)) # Given result = ResultCollecter() # When case(result) # Then self.assertTrue(result.wasSuccessful())
def test_load_invalid_yaml_tests(self): # Given test_yaml = textwrap.dedent(""" --- version: '1.0' config: host: test.domain tests: - name: "Test root URL" url: "/" """) test_data = yaml.safe_load(test_yaml) # When suite = self.loader.load_tests_from_yaml(test_data, '/path/to/foo.yaml') # Then self.assertIsInstance(suite, TestSuite) self.assertEqual(suite.countTestCases(), 1) case = next(find_test_cases(suite)) self.assertIsInstance(case, unittest.TestCase) self.assertIsInstance(case, ModuleImportError) # Given result = ResultCollecter() # When/Then meth = getattr(case, case._testMethodName) with self.assertRaises(YamlParseError): meth() # When case(result) # Then self.assertFalse(result.wasSuccessful()) self.assertEqual(len(result.errors), 1)
def test_load_invalid_yaml_tests(self): # Given test_yaml = textwrap.dedent(""" --- version: '1.0' config: host: test.domain tests: - name: "Test root URL" url: "/" """) test_data = yaml.safe_load(test_yaml) # When suite = self.loader.load_tests_from_yaml( test_data, '/path/to/foo.yaml') # Then self.assertIsInstance(suite, TestSuite) self.assertEqual(suite.countTestCases(), 1) case = next(find_test_cases(suite)) self.assertIsInstance(case, unittest.TestCase) self.assertIsInstance(case, ModuleImportError) # Given result = ResultCollecter() # When/Then meth = getattr(case, case._testMethodName) with self.assertRaises(YamlParseError): meth() # When case(result) # Then self.assertFalse(result.wasSuccessful()) self.assertEqual(len(result.errors), 1)
def run_test(import_dir, repositories, verbose, strict): loader = Loader() click.echo('Generating test cases...') test_cases = [ loader.load_case(generate_tests(import_dir, repository, strict)) for repository in repositories ] suite = loader.create_suite(test_cases) click.echo('Done.') test_count = suite.countTestCases() if verbose: result_handler = VerboseTestResultHandler(test_count=test_count) else: result_handler = StandardTestResultHandler(test_count=test_count) result_collector = ResultCollecter(buffer=False, failfast=False) result_collector.add_result_handler(result_handler) runner = BaseTestRunner() result = runner.run(result_collector, suite) return result.wasSuccessful()
def test_case_setup(self): # Given test_yaml = textwrap.dedent(""" version: '1.0' config: host: test.domain test-pre-definitions: some_tests: - name: "Root" url: "/" more_tests: - name: "Post-1" url: "/one" - name: "Post-2" url: "/two" cases: - name: "Basic" case-setup: - some_tests case-teardown: - more_tests tests: - name: "Another URL" url: "/another" """) responses.add( responses.GET, 'http://test.domain/', status=200, ) responses.add( responses.GET, 'http://test.domain/another', status=201, ) responses.add( responses.GET, 'http://test.domain/one', status=202, ) responses.add( responses.GET, 'http://test.domain/two', status=203, ) test_data = yaml.safe_load(test_yaml) # When suite = self.loader.load_tests_from_yaml(test_data, '/path/to/foo.yaml') # Then self.assertIsInstance(suite, TestSuite) self.assertEqual(suite.countTestCases(), 4) # When result = ResultCollecter() suite(result) # Then self.assertEqual(len(responses.calls), 4) self.assertTrue(result.wasSuccessful()) pre, test, post_1, post_2 = responses.calls self.assertEqual(pre.request.url, 'http://test.domain/') self.assertEqual(test.request.url, 'http://test.domain/another') self.assertEqual(post_1.request.url, 'http://test.domain/one') self.assertEqual(post_2.request.url, 'http://test.domain/two')
def test_case_setup(self): # Given test_yaml = textwrap.dedent(""" version: '1.0' config: host: test.domain test-pre-definitions: some_tests: - name: "Root" url: "/" more_tests: - name: "Post-1" url: "/one" - name: "Post-2" url: "/two" cases: - name: "Basic" case-setup: - some_tests case-teardown: - more_tests tests: - name: "Another URL" url: "/another" """) responses.add( responses.GET, 'http://test.domain/', status=200, ) responses.add( responses.GET, 'http://test.domain/another', status=201, ) responses.add( responses.GET, 'http://test.domain/one', status=202, ) responses.add( responses.GET, 'http://test.domain/two', status=203, ) test_data = yaml.safe_load(test_yaml) # When suite = self.loader.load_tests_from_yaml( test_data, '/path/to/foo.yaml') # Then self.assertIsInstance(suite, TestSuite) self.assertEqual(suite.countTestCases(), 4) # When result = ResultCollecter() suite(result) # Then self.assertEqual(len(responses.calls), 4) self.assertTrue(result.wasSuccessful()) pre, test, post_1, post_2 = responses.calls self.assertEqual(pre.request.url, 'http://test.domain/') self.assertEqual(test.request.url, 'http://test.domain/another') self.assertEqual(post_1.request.url, 'http://test.domain/one') self.assertEqual(post_2.request.url, 'http://test.domain/two')