def test_error(self): class MyException(Exception): pass c = Checker() with self.assertRaises(MyException): with c.expect("Everything will be fine"): raise MyException("It wasn't fine!") self.assertEqual(self.clean_results(c.results), [ { 'status': 'ERROR', 'expect': "Everything will be fine", 'did': "It wasn't fine!", 'exception': { 'type': "MyException", 'message': "It wasn't fine!", 'readable': "MyException: It wasn't fine!", 'traceback': [ { 'file': 'checker/tests/test_run.py', 'line': 0, 'function': 'test_error', 'text': 'raise MyException("It wasn\'t fine!")' }, ] }, }, ])
def test_quiet_expects(self): c = Checker() with c.expect("It isn't even worth mentioning.", quiet=True): pass with c.expect("Let's talk about this."): pass self.assertEqual(c.results, [{"status": "OK", "expect": "Let's talk about this."}])
def test_error(self): class MyException(Exception): pass c = Checker() with self.assertRaises(MyException): with c.expect("Everything will be fine"): raise MyException("It wasn't fine!") self.assertEqual( self.clean_results(c.results), [ { "status": "ERROR", "expect": "Everything will be fine", "did": "It wasn't fine!", "exception": { "type": "MyException", "message": "It wasn't fine!", "readable": "MyException: It wasn't fine!", "traceback": [ { "file": "checker/tests/test_run.py", "line": 0, "function": "test_error", "text": 'raise MyException("It wasn\'t fine!")', } ], }, } ], )
def test_flaky_function(self): c = Checker() c.function_returns(self, "flaky", [(2, 2), (3, 3), (4, 4)]) self.assertEqual( self.clean_results(c.results), [ {"status": "OK", "expect": "flaky(2) should return 2."}, { "status": "ERROR", "expect": "flaky(3) should return 3.", "did": "Oops", "exception": { "type": "Flake", "message": "Oops", "readable": "Flake: Oops", "traceback": [ { "file": "checker/exerciser.py", "line": 0, "function": "function_returns", "text": "actual_output = fn(*inputs)", }, { "file": "checker/tests/test_run.py", "line": 0, "function": "flaky", "text": 'raise self.Flake("Oops")', }, ], }, }, {"status": "OK", "expect": "flaky(4) should return 4."}, ], )
def test_success(self): c = Checker() try: with c.expect("This should work"): pass except Checker.Done: # pragma: nocover self.fail("Shouldn't have raised Done here.") self.assertEqual(c.results, [{"status": "OK", "expect": "This should work"}])
def test_no_such_function(self): c = Checker() with self.assertRaises(Checker.Done): c.function_returns(self, 'nothing', [(1, 2), (2, 4)]) self.assertEqual(c.results, [ { 'status': 'FAIL', 'expect': 'You should have a function named nothing.' }, ])
def test_failure(self): c = Checker() try: with c.expect("This should work"): c.fail("It failed!") self.fail("We shouldn't have run past a c.fail") # pragma: nocover self.fail("We shouldn't have continued after a failed c.should") # pragma: nocover except Checker.Done: pass self.assertEqual(c.results, [{"status": "FAIL", "expect": "This should work", "did": "It failed!"}])
def test_simple(self): c = Checker() c.function_returns(self, "simple", [(1, 2), (2, 4)]) self.assertEqual( c.results, [ {"status": "OK", "expect": "simple(1) should return 2."}, {"status": "OK", "expect": "simple(2) should return 4."}, ], )
def test_not_callable(self): c = Checker() self.mylist = [] with self.assertRaises(Checker.Done): c.function_returns(self, 'mylist', [(1, 2), (2, 4)]) self.assertEqual(c.results, [ { 'status': 'FAIL', 'expect': 'You should have a function named mylist.' }, ])
def test_multiple_arguments(self): c = Checker() c.function_returns(self, "add3", [(1, 2, 3, 6), (1, 1, 1, 3), (10, 11, 12, 33)]) self.assertEqual( c.results, [ {"status": "OK", "expect": "add3(1, 2, 3) should return 6."}, {"status": "OK", "expect": "add3(1, 1, 1) should return 3."}, {"status": "OK", "expect": "add3(10, 11, 12) should return 33."}, ], )
def test_wrong_answers(self): c = Checker() c.function_returns(self, "simple", [(1, 2), (2, 17), (3, 6)]) self.assertEqual( c.results, [ {"status": "OK", "expect": "simple(1) should return 2."}, {"status": "FAIL", "expect": "simple(2) should return 17.", "did": "You returned 4."}, {"status": "OK", "expect": "simple(3) should return 6."}, ], )
def test_quiet_expects(self): c = Checker() with c.expect("It isn't even worth mentioning.", quiet=True): pass with c.expect("Let's talk about this."): pass self.assertEqual(c.results, [ { 'status': 'OK', 'expect': "Let's talk about this." }, ])
def test_simple(self): c = Checker() c.function_returns(self, 'simple', [(1, 2), (2, 4)]) self.assertEqual(c.results, [ { 'status': 'OK', 'expect': 'simple(1) should return 2.' }, { 'status': 'OK', 'expect': 'simple(2) should return 4.' }, ])
def test_success(self): c = Checker() try: with c.expect("This should work"): pass except Checker.Done: # pragma: nocover self.fail("Shouldn't have raised Done here.") self.assertEqual(c.results, [ { 'status': 'OK', 'expect': "This should work" }, ])
def test_failure_with_continue_on_fail(self): c = Checker() try: with c.expect("This should work", continue_on_fail=True): c.fail("It failed!") self.fail("We shouldn't have run past a c.fail") # pragma: nocover with c.expect("Also this one"): pass except Checker.Done: # pragma: nocover self.fail("Shouldn't have raised Done here.") self.assertEqual( c.results, [ {"status": "FAIL", "expect": "This should work", "did": "It failed!"}, {"status": "OK", "expect": "Also this one"}, ], )
def test_failure(self): c = Checker() try: with c.expect("This should work"): c.fail("It failed!") self.fail( "We shouldn't have run past a c.fail") # pragma: nocover self.fail("We shouldn't have continued after a failed c.should" ) # pragma: nocover except Checker.Done: pass self.assertEqual(c.results, [ { 'status': 'FAIL', 'expect': "This should work", 'did': "It failed!" }, ])
def test_multiple_arguments(self): c = Checker() c.function_returns(self, 'add3', [(1, 2, 3, 6), (1, 1, 1, 3), (10, 11, 12, 33)]) self.assertEqual(c.results, [ { 'status': 'OK', 'expect': 'add3(1, 2, 3) should return 6.' }, { 'status': 'OK', 'expect': 'add3(1, 1, 1) should return 3.' }, { 'status': 'OK', 'expect': 'add3(10, 11, 12) should return 33.' }, ])
def test_wrong_answers(self): c = Checker() c.function_returns(self, 'simple', [(1, 2), (2, 17), (3, 6)]) self.assertEqual(c.results, [ { 'status': 'OK', 'expect': 'simple(1) should return 2.' }, { 'status': 'FAIL', 'expect': 'simple(2) should return 17.', 'did': 'You returned 4.' }, { 'status': 'OK', 'expect': 'simple(3) should return 6.' }, ])
def test_flaky_function(self): c = Checker() c.function_returns(self, 'flaky', [(2, 2), (3, 3), (4, 4)]) self.assertEqual(self.clean_results(c.results), [ { 'status': 'OK', 'expect': 'flaky(2) should return 2.' }, { 'status': 'ERROR', 'expect': "flaky(3) should return 3.", 'did': 'Oops', 'exception': { 'type': 'Flake', 'message': 'Oops', 'readable': 'Flake: Oops', 'traceback': [ { 'file': 'checker/exerciser.py', 'line': 0, 'function': 'function_returns', 'text': 'actual_output = fn(*inputs)' }, { 'file': 'checker/tests/test_run.py', 'line': 0, 'function': 'flaky', 'text': 'raise self.Flake("Oops")' }, ] }, }, { 'status': 'OK', 'expect': 'flaky(4) should return 4.' }, ])
def test_test(self): c = Checker() try: with c.expect("This should definitely work"): c.test(True, "This one was ok") c.test(False, "Oops, this was bad") raise Exception("Shouldn't have gotten to here") # pragma: nocover except Checker.Done: pass self.assertEqual( c.results, [{"status": "FAIL", "expect": "This should definitely work", "did": "Oops, this was bad"}] )
def test_test(self): c = Checker() try: with c.expect("This should definitely work"): c.test(True, "This one was ok") c.test(False, "Oops, this was bad") raise Exception( "Shouldn't have gotten to here") # pragma: nocover except Checker.Done: pass self.assertEqual(c.results, [ { 'status': 'FAIL', 'expect': "This should definitely work", 'did': "Oops, this was bad" }, ])
def test_failure_with_continue_on_fail(self): c = Checker() try: with c.expect("This should work", continue_on_fail=True): c.fail("It failed!") self.fail( "We shouldn't have run past a c.fail") # pragma: nocover with c.expect("Also this one"): pass except Checker.Done: # pragma: nocover self.fail("Shouldn't have raised Done here.") self.assertEqual(c.results, [ { 'status': 'FAIL', 'expect': "This should work", 'did': "It failed!" }, { 'status': 'OK', 'expect': "Also this one" }, ])
def test_no_such_function(self): c = Checker() with self.assertRaises(Checker.Done): c.function_returns(self, "nothing", [(1, 2), (2, 4)]) self.assertEqual(c.results, [{"status": "FAIL", "expect": "You should have a function named nothing."}])
def test_not_callable(self): c = Checker() self.mylist = [] with self.assertRaises(Checker.Done): c.function_returns(self, "mylist", [(1, 2), (2, 4)]) self.assertEqual(c.results, [{"status": "FAIL", "expect": "You should have a function named mylist."}])