def test_run_tests2(self): """ Test cases should be failable """ start = timezone.now() testcase = rfactories.TestCaseF.create() rfactories.TestCaseF.create(testrun=testcase.testrun) step1 = rfactories.TestCaseStepF.create(testcase=testcase, order=1) rfactories.TestCaseAssertF.create(step=step1, lhs='.text', rhs='Wazup!', operator='eq', order=1) rfactories.TestCaseAssertF.create(step=step1, lhs='.status_code', rhs='200', operator='eq', order=2) step2 = rfactories.TestCaseStepF.create(testcase=testcase, url='groups.json', order=2) rfactories.TestCaseAssertF.create(step=step2, lhs='.status_code', rhs='200', operator='eq') rfactories.TestCaseAssertF.create(step=step2, lhs='.text', rhs='Hello, world!', operator='eq') run_tests(testcase.testrun.pk) self.assertEqual(1, requests.sessions.Session.request.call_count) self.assertEqual(1, rmodels.TestRun.objects.count()) self.assertObject(rmodels.TestRun.objects.all()[0], result=rmodels.TestResult.fail) self.assertEqual(2, rmodels.TestCase.objects.count()) self.assertObject(rmodels.TestCase.objects.all()[0], start_date__gte=start, end_date__gte=start, result=rmodels.TestResult.fail) self.assertObject(rmodels.TestCase.objects.all()[1], start_date__gte=start, end_date__gte=start, result=rmodels.TestResult.success) self.assertEqual(2, rmodels.TestCaseStep.objects.count()) self.assertObject(rmodels.TestCaseStep.objects.all()[0], result=rmodels.TestResult.fail, exception='AssertionError: 404 should be eq 200')
def test_run_tests3(self): """ Testcases can raise errors, and they are handled """ start = timezone.now() testcase = rfactories.TestCaseF.create() step1 = rfactories.TestCaseStepF.create(testcase=testcase, order=1) rfactories.TestCaseAssertF.create(step=step1, lhs='.text', rhs='Wazup!', operator='eq', order=1) rfactories.TestCaseAssertF.create(step=step1, lhs='.status_code', rhs='200', operator='eq', order=2) step2 = rfactories.TestCaseStepF.create(testcase=testcase, url='groups.json', order=2) rfactories.TestCaseAssertF.create(step=step2, lhs='.status_code', rhs='200', operator='eq') rfactories.TestCaseAssertF.create(step=step2, lhs='.text', rhs='Hello, world!', operator='eq') run_tests(testcase.testrun.pk) self.assertEqual(1, requests.sessions.Session.request.call_count) self.assertEqual(1, rmodels.TestCase.objects.count()) self.assertObject(rmodels.TestCase.objects.all()[0], start_date__gte=start, end_date__gte=start, result=rmodels.TestResult.error) self.assertEqual(2, rmodels.TestCaseStep.objects.count()) self.assertObject(rmodels.TestCaseStep.objects.all()[0], result=rmodels.TestResult.error, exception='Timeout: bad for you')
def test_runs_testcase(self): """ Should call TestRun.run """ testrun = rfactories.TestRunF.create() run_tests(testrun.pk) rmodels.TestRun.run.assert_called_once_with()
def test_run_tests(self): """ Testcases should be passable """ start = timezone.now() testcase = rfactories.TestCaseF.create() rfactories.TestCaseF.create(testrun=testcase.testrun) step1 = rfactories.TestCaseStepF.create(testcase=testcase, order=1) step2 = rfactories.TestCaseStepF.create(testcase=testcase, url='groups.json', order=2) rfactories.TestCaseAssertF.create(step=step2, lhs='.status_code', rhs='200', operator='eq', order=1) rfactories.TestCaseAssertF.create(step=step2, lhs='.text', rhs='Hello, world!', operator='eq', order=2) def my_side_effect(*args, **kwargs): if args[0] == 'GET' and args[1] == '%s%s' % (testcase.testrun.base_url, step1.url): return ResponseF() elif args[0] == 'GET' and args[1] == '%s%s' % (testcase.testrun.base_url, step2.url): return ResponseF(content='Hello, world!') else: raise AssertionError('Unexpected requests.request arguments: %s, %s' % (args, kwargs)) with mock.patch('requests.sessions.Session.request', mock.Mock(side_effect=my_side_effect)): run_tests(testcase.testrun.pk) self.assertEqual(2, requests.sessions.Session.request.call_count) self.assertEqual(1, rmodels.TestRun.objects.count()) self.assertObject(rmodels.TestRun.objects.all()[0], result=rmodels.TestResult.success) self.assertEqual(2, rmodels.TestCase.objects.count()) self.assertObject(rmodels.TestCase.objects.all()[0], start_date__gte=start, end_date__gte=start, result=rmodels.TestResult.success) self.assertObject(rmodels.TestCase.objects.all()[1], start_date__gte=start, end_date__gte=start, result=rmodels.TestResult.success)
def test_non_existing_test(self): """ Should raise emodels.TestCase.DoesNotExist if test case not found. """ with self.assertRaises(rmodels.TestRun.DoesNotExist): run_tests(10000)