def test_if_smoke_test_fails_on_custom_disallowed_response_status_codes( self, http_method): random_status_code = random.randint(100, 510) custom_disallowed_status_codes = [ random_status_code, random_status_code + 1 ] tests_generator = SmokeTestsGenerator( disallowed_status_codes=custom_disallowed_status_codes) with patch('django.test.client.Client.{}'.format( http_method.lower())) as mocked_method: # return different status code than disallowed mocked_method.return_value = HttpResponse( status=custom_disallowed_status_codes[0]) # use new endpoint to be sure that test was not created in previous tests endpoint_url = '/{}'.format(create_random_string()) expected_test_name = tests_generator.create_test_name( http_method, endpoint_url) tests_generator.create_test_for_http_method( http_method, endpoint_url) # check if test was created and added to test class self.assertTrue(hasattr(SmokeTests, expected_test_name)) is_successful, failures, skipped = self._execute_smoke_test( expected_test_name) self.assertFalse(is_successful) self.assertEqual(len(failures), 1) self.assertEqual(skipped, []) self.assertEqual(tests_generator.warnings, [])
def test_if_error_is_raised_when_app_is_not_in_installed_apps( self, mocked_call_command): with self.assertRaises(AppNotInInstalledApps): tests_generator = SmokeTestsGenerator( app_names=[create_random_string()]) tests_generator.execute() mocked_call_command.assert_not_called()
def test_if_smoke_test_fails_on_500_response_status_code( self, http_method): """ Check if smoke test fails when gets 500 status code from endpoint's response. """ with patch('django.test.client.Client.{}'.format( http_method.lower())) as mocked_method: mocked_method.return_value = HttpResponse(status=500) # use new endpoint to be sure that test was not created in previous tests endpoint_url = '/{}'.format(create_random_string()) expected_test_name = self.tests_generator.create_test_name( http_method, endpoint_url) self.tests_generator.create_test_for_http_method( http_method, endpoint_url) # check if test was created and added to test class self.assertTrue(hasattr(SmokeTests, expected_test_name)) is_successful, failures, skipped = self._execute_smoke_test( expected_test_name) self.assertFalse(is_successful) self.assertEqual(len(failures), 1) self.assertEqual(skipped, []) self.assertEqual(self.tests_generator.warnings, [])
def test_create_skipped_test_for_not_supported_endpoint( self, http_method, mocked_normalize): mocked_normalize.return_value = [] tests_generator = SmokeTestsGenerator() url_pattern = URLPattern(r'^{}$'.format(create_random_string()), RedirectView.as_view(url='/', permanent=False), name=create_random_string()) expected_test_name = tests_generator.create_test_name( http_method, get_pattern(url_pattern)) tests_generator.create_tests_for_endpoint(get_pattern(url_pattern), url_pattern.name) self.assertTrue(hasattr(SmokeTests, expected_test_name)) is_successful, failures, skipped = self._execute_smoke_test( expected_test_name) self.assertEqual(len(skipped), 1) self.assertEqual(len(tests_generator.warnings), 1)
def test_if_test_without_db_is_successful(self): tests_generator = SmokeTestsGenerator(use_db=False) http_method = 'GET' endpoint_url = '/{}'.format(create_random_string()) expected_test_name = self.tests_generator.create_test_name( http_method, endpoint_url) tests_generator.create_test_for_http_method(http_method, endpoint_url) is_successful, failures, skipped = self._execute_smoke_test( expected_test_name) self.assertTrue(is_successful) self.assertEqual(failures, []) self.assertEqual(tests_generator.warnings, [])
def test_smoke_test_is_created_only_for_specified_app( self, mocked_call_command): outside_app_url_pattern = URLPattern( r'^{}$'.format(create_random_string()), RedirectView.as_view(url='/', permanent=False), name=create_random_string()) outside_app_test_name = self.tests_generator.create_test_name( 'GET', get_pattern(outside_app_url_pattern)) inside_app_url_pattern = app_url_patterns[0] inside_app_url_full_pattern = '^app_urls/' + get_pattern( inside_app_url_pattern) inside_app_test_name = self.tests_generator.create_test_name( 'GET', inside_app_url_full_pattern) tests_generator = SmokeTestsGenerator(app_names=['tests.app']) tests_generator.execute() self.assertFalse(hasattr(SmokeTests, outside_app_test_name)) self.assertTrue(hasattr(SmokeTests, inside_app_test_name)) mocked_call_command.assert_called_once_with('test', 'django_smoke_tests')
def test_no_db_test_runner(self): tests_generator = SmokeTestsGenerator(use_db=False) http_method = 'GET' endpoint_url = '/{}'.format(create_random_string()) expected_test_name = self.tests_generator.create_test_name( http_method, endpoint_url) tests_generator.create_test_for_http_method(http_method, endpoint_url) suite = unittest.TestSuite() suite.addTest(SmokeTests(expected_test_name)) with captured_output() as (_, _): # skip output test_runner = NoDbTestRunner(stream=DummyStream, verbosity=-1).run_suite(suite) self.assertEqual(test_runner.errors, []) self.assertTrue(test_runner.wasSuccessful()) self.assertEqual(test_runner.failures, []) self.assertEqual(test_runner.skipped, [])
def test_if_smoke_test_fails_on_allowed_response_status_code( self, http_method): # use new endpoint to be sure that test was not created in previous tests endpoint_url = '/{}'.format(create_random_string()) expected_test_name = self.tests_generator.create_test_name( http_method, endpoint_url) self.tests_generator.create_test_for_http_method( http_method, endpoint_url, detail_url=True) # detail_url set to True to allow 404 # check if test was created and added to test class self.assertTrue(hasattr(SmokeTests, expected_test_name)) is_successful, failures, skipped = self._execute_smoke_test( expected_test_name) self.assertTrue(is_successful) self.assertEqual(failures, []) self.assertEqual(skipped, []) self.assertEqual(self.tests_generator.warnings, [])
def test_if_fixture_is_applied(self, call_command_for_test, call_command_for_loaddata): fixture_path = 'file.json' tests_generator = SmokeTestsGenerator(fixture_path=fixture_path) tests_generator.execute() http_method = 'GET' endpoint_url = '/{}'.format(create_random_string()) expected_test_name = self.tests_generator.create_test_name( http_method, endpoint_url) tests_generator.create_test_for_http_method(http_method, endpoint_url) is_successful, failures, skipped = self._execute_smoke_test( expected_test_name) self.assertTrue(is_successful) self.assertEqual(skipped, []) self.assertEqual(failures, []) call_command_for_test.assert_called_once_with('test', 'django_smoke_tests') call_command_for_loaddata.assert_called_once_with( 'loaddata', fixture_path)