def test_raises_when_no_provider(self): clear() test(data_provider="missing")(valid_for_provider) with self.assertRaises(UnknownProviderName) as e: start(TestSuite.get_instance(), listener=Listener(0)) self.assertTrue("Could not find provider(s) named ['missing']" in e.exception.args[0])
def test_empty_suite_call(self): clear() listener_ = Listener(0) listener_.on_empty_suite = inc count = COUNT r_.start(listener=listener_) self.assertEqual(count + 1, COUNT)
def test_parallel(self): clear() test(groups=('one', ))(half_second) test(groups=('two', ))(half_second) st = time() start(listener=Listener(0), threads=2) self.assertTrue(time() - st < 1)
def test_soft_failed(self): clear() test(sa_failed) start(listener=Listener(0)) self.assertEqual(0, len(TestSuite.get_instance().broken())) self.assertEqual(0, len(TestSuite.get_instance().success())) self.assertEqual(1, len(TestSuite.get_instance().failed())) self.assertEqual(1, TestSuite.get_instance().tests_count())
def test_ends_call(self): clear() listener_ = Listener(0) listener_.on_suite_ends = inc count = COUNT test(fake) r_.start(listener=listener_) self.assertEqual(count + 1, COUNT)
def test_dry_run_call(self): clear() listener_ = Listener(0) listener_.on_dry_run = inc count = COUNT test(fake) r_.start(listener=listener_, dry_run=True) self.assertEqual(count + 1, COUNT)
def test_ignored_by_condition_call(self): clear() listener_ = Listener(0) listener_.on_ignored_by_condition = inc count = COUNT test(only_if=lambda: False)(fake) r_.start(listener=listener_) self.assertEqual(count + 1, COUNT)
def test_is_run_if_only_true(self): clear() test(only_if=lambda: True)(sa_ok) start(listener=Listener(0)) self.assertEqual(0, len(TestSuite.get_instance().broken())) self.assertEqual(1, len(TestSuite.get_instance().success())) self.assertEqual(0, len(TestSuite.get_instance().failed())) self.assertEqual(1, TestSuite.get_instance().tests_count())
def test_timeout_ok_if_less(self): clear() test(timeout=10)(simple) start(listener=Listener(0)) self.assertEqual(0, len(TestSuite.get_instance().broken())) self.assertEqual(1, len(TestSuite.get_instance().success())) self.assertEqual(0, len(TestSuite.get_instance().failed())) self.assertEqual(1, TestSuite.get_instance().tests_count())
def test_timeout_failed_if_bigger(self): clear() test(timeout=1)(long) start(listener=Listener(0)) self.assertEqual(1, len(TestSuite.get_instance().broken())) self.assertEqual(0, len(TestSuite.get_instance().success())) self.assertEqual(0, len(TestSuite.get_instance().failed())) self.assertEqual(1, TestSuite.get_instance().tests_count())
def test_starts_call(self): clear() reload(r_) listener_ = Listener(0) listener_.on_test_starts = inc count = COUNT test(fake) r_.start(listener=listener_) self.assertEqual(count + 1, COUNT)
def test_on_max_fail_not_call(self): clear() listener_ = Listener(0) listener_.on_suite_stop_with_max_fail = inc count = COUNT test(fail) test(fail) r_.start(listener=listener_, max_fail=3) self.assertEqual(count, COUNT)
def test_before_suite_failed_call(self): clear() listener_ = Listener(0) listener_.on_before_suite_failed = inc count = COUNT before_suite(lambda: int('a')) test(fake) r_.start(listener=listener_) self.assertEqual(count + 1, COUNT)
def test_ignored_call(self): clear() listener_ = Listener(0) listener_.on_ignored = inc count = COUNT test(fake) before(lambda: int('a')) r_.start(listener=listener_) self.assertEqual(count + 1, COUNT)
def test_provider_get_none(self): clear() CONTAINER([1, None], name='three') test(data_provider="three")(_fn) start(listener=Listener(0)) self.assertEqual(0, len(TestSuite.get_instance().broken())) self.assertEqual(1, len(TestSuite.get_instance().success())) self.assertEqual(1, len(TestSuite.get_instance().failed())) self.assertEqual(2, TestSuite.get_instance().tests_count())
def test_timeout_failed_with_data(self): clear() provider(name='three')(return2) test(timeout=1, data_provider="three")(long_data) start(listener=Listener(0)) self.assertEqual(3, len(TestSuite.get_instance().broken())) self.assertEqual(0, len(TestSuite.get_instance().success())) self.assertEqual(0, len(TestSuite.get_instance().failed())) self.assertEqual(3, TestSuite.get_instance().tests_count())
def test_provider_str_arg(self): def _(): return [[0, 1]] clear() provider(name='some')(_) test(data_provider='some')(_fn) start(listener=Listener(0)) self.assertEqual(TestSuite.get_instance().failed()[0].str_arg, '[0, 1]')
def test_name_filter_empty(self): clear() test(name='one')(sa_ok) test(name='ane')(sa_ok) start(listener=Listener(0), filter_by_name='empty') self.assertEqual(0, len(TestSuite.get_instance().broken())) self.assertEqual(0, len(TestSuite.get_instance().success())) self.assertEqual(0, len(TestSuite.get_instance().failed())) self.assertEqual(0, len(TestSuite.get_instance().ignored())) self.assertEqual(0, TestSuite.get_instance().tests_count())
def test__with_fail(self): def _if2(): test_fail() clear() test(_if2) start(listener=Listener(0)) self.assertEqual(0, len(TestSuite.get_instance().broken())) self.assertEqual(0, len(TestSuite.get_instance().success())) self.assertEqual(1, len(TestSuite.get_instance().failed())) self.assertEqual(1, TestSuite.get_instance().tests_count())
def test_failed_call(self): def fail(): assert 1 == 2 clear() listener_ = Listener(0) listener_.on_failed = inc count = COUNT test(fail) r_.start(listener=listener_) self.assertEqual(count + 1, COUNT)
def test_broken_call(self): def broke(): int('1a') clear() listener_ = Listener(0) listener_.on_broken = inc count = COUNT test(broke) r_.start(listener=listener_) self.assertEqual(count + 1, COUNT)
def test_three_success_one_failed_when_data(self): def _(): return [0, 1, 2, 3] clear() provider(name='four')(_) test(data_provider='four')(_fn) start(listener=Listener(0)) self.assertEqual(0, len(TestSuite.get_instance().broken())) self.assertEqual(3, len(TestSuite.get_instance().success())) self.assertEqual(1, len(TestSuite.get_instance().failed())) self.assertEqual(4, TestSuite.get_instance().tests_count())
def test_ignore_with_provider_call(self): def wrong(): return 1 clear() listener_ = Listener(0) listener_.on_ignored_with_provider = inc count = COUNT provider(name='prov')(wrong) test(data_provider='prov')(lambda x: x) r_.start(listener=listener_) self.assertEqual(count + 1, COUNT)
def test_provider_with_brake(self): def _if2(it): if it == 2: test_break() clear() CONTAINER([1, 2, 3, 4], name='three') test(data_provider="three")(_if2) start(listener=Listener(0)) self.assertEqual(1, len(TestSuite.get_instance().broken())) self.assertEqual(3, len(TestSuite.get_instance().success())) self.assertEqual(0, len(TestSuite.get_instance().failed())) self.assertEqual(4, TestSuite.get_instance().tests_count())
def test_ignored_on_sys_exit(self): clear() def exit_(): import sys sys.exit(1) test(exit_) start(listener=Listener(0)) self.assertEqual(0, len(TestSuite.get_instance().broken())) self.assertEqual(0, len(TestSuite.get_instance().success())) self.assertEqual(0, len(TestSuite.get_instance().failed())) self.assertEqual(1, len(TestSuite.get_instance().ignored())) self.assertEqual(1, TestSuite.get_instance().tests_count())
def test_provider_cached(self): count = 0 def ca_(): nonlocal count count += 1 return [0, 1] clear() provider(cached=True)(ca_) test(data_provider="ca_")(_fn) test(data_provider="ca_", name='teo')(_fn) start(listener=Listener(0)) self.assertEqual(1, count) self.assertEqual(0, len(TestSuite.get_instance().broken())) self.assertEqual(4, len(TestSuite.get_instance().success())) self.assertEqual(0, len(TestSuite.get_instance().failed())) self.assertEqual(4, TestSuite.get_instance().tests_count())
class TestAnnotations(TestCase): _listener = Listener(0) def test_valid_func_works(self): clear() initial_count = TestSuite.get_instance().tests_count() test(valid) self.assertEqual(initial_count + 1, TestSuite.get_instance().tests_count()) def test_not_add_if_disabled(self): clear() initial_count = TestSuite.get_instance().tests_count() test(enabled=False)(valid) self.assertEqual(initial_count, TestSuite.get_instance().tests_count()) def test_not_add_if_enabled(self): clear() initial_count = TestSuite.get_instance().tests_count() test(enabled=True)(valid) self.assertEqual(initial_count + 1, TestSuite.get_instance().tests_count()) def test_raises_when_func_for_provider_has_no_arg(self): with self.assertRaises(WrongDecoratedObject) as e: test(data_provider="good")(valid) self.assertEqual( "Test named 'valid' uses a data provider, but takes no arguments.", e.exception.args[0]) def test_raises_when_no_provider(self): clear() test(data_provider="missing")(valid_for_provider) with self.assertRaises(UnknownProviderName) as e: start(TestSuite.get_instance(), listener=Listener(0)) self.assertTrue("Could not find provider(s) named ['missing']" in e.exception.args[0]) def test_ignore_all_if_test_disabled(self): test(enabled=False, data_provider="wrong")(valid) def test_raises_when_using_test_with_class(self): with self.assertRaises(WrongDecoratedObject) as ex: test(T) self.assertTrue('test' in ex.exception.args[0]) def test_raises_when_using_function_with_arg(self): with self.assertRaises(WrongDecoratedObject) as ex: test(valid_for_provider) self.assertTrue('test' in ex.exception.args[0]) def test_raises_when_using_test_with_class_methods(self): with self.assertRaises(WrongDecoratedObject) as ex: t = T() test(t.m) self.assertTrue('test' in ex.exception.args[0]) def test_name_param_works(self): clear() test(name='new_name')(valid) self.assertTrue('new_name' in [ t.name for t in list(TestSuite.get_instance().groups.values())[0].tests ]) def test_raises_if_only_is_not_callable(self): clear() with self.assertRaises(ValueError): test(only_if=False)(valid) def test_data_works(self): clear() provider(name="any_name")(valid_for_data) self.assertTrue('any_name' in TestSuite.get_instance().providers) def test_data_raises_when_duplicate_name(self): with self.assertRaises(DuplicateProviderNameException) as e: provider(valid_for_data) provider(valid_for_data) self.assertEqual( "Provider named 'valid_for_data' already exists. Provider names must be unique.", e.exception.args[0]) def test_data_raises_when_two_args(self): def _(): return [1, 2] with self.assertRaises(WrongDecoratedObject) as e: provider(name='another2')(_) test(data_provider='another2')(non_valid_for_provider) self.assertEqual( "Test named 'non_valid_for_provider' uses a data provider, but takes more than one argument.", e.exception.args[0]) def test_data_name_works(self): clear() provider(name="another")(valid_for_data) self.assertTrue('another' in TestSuite.get_instance().providers) def test_data_ignore_if_disabled(self): clear() provider(enabled=False, name="no")(valid) self.assertFalse('no' in TestSuite.get_instance().providers) def test_before_all_when_before_first(self): clear() before_ = valid before(before_) t = Test('first', print) list(TestSuite.get_instance().groups.values())[0].add_test(t) self.assertTrue(t.before) self.assertEqual(before_, t.before[0]) def test_before_all_when_before_last(self): clear() t = Test('second', print) TestSuite.get_instance().get_or_create("new").add_test(t) bef = lambda: None before(group_name='new')(bef) self.assertTrue(bef in t.before) def test_no_timeout_default(self): clear() test(valid) self.assertEqual( 0, list(TestSuite.get_instance().groups.values())[0].tests[0].timeout) def test_timeout_same(self): clear() test(timeout=10)(valid) self.assertEqual( 10, list(TestSuite.get_instance().groups.values())[0].tests[0].timeout) def test_timeout_same_float(self): clear() test(timeout=10.02)(valid) self.assertEqual( 10, list(TestSuite.get_instance().groups.values())[0].tests[0].timeout) def test_timeout_same_negative(self): clear() test(timeout=-1)(valid) self.assertEqual( 0, list(TestSuite.get_instance().groups.values())[0].tests[0].timeout) def test_description_empty(self): clear() test(valid) self.assertIsNone( list(TestSuite.get_instance().groups.values()) [0].tests[0].description) self.assertEqual( 'annotations_test.valid', str(list(TestSuite.get_instance().groups.values())[0].tests[0])) def test_description_from_param(self): clear() test(description='test')(valid) self.assertEqual( list(TestSuite.get_instance().groups.values()) [0].tests[0].description, 'test') self.assertEqual( "annotations_test.valid ('test')", str(list(TestSuite.get_instance().groups.values())[0].tests[0])) def test_description_from_docs(self): clear() test(f) self.assertEqual( list(TestSuite.get_instance().groups.values()) [0].tests[0].description, 'Test') self.assertEqual( "annotations_test.f ('Test')", str(list(TestSuite.get_instance().groups.values())[0].tests[0])) def test_description_from_param_wins_docs(self): clear() test(description='test')(f) self.assertEqual( list(TestSuite.get_instance().groups.values()) [0].tests[0].description, 'test') self.assertEqual( "annotations_test.f ('test')", str(list(TestSuite.get_instance().groups.values())[0].tests[0])) def test_data_file(self): clear() DATA_FILE('test_data.txt', name='new') test(data_provider='new')(valid_for_provider) self.assertTrue('new' in TestSuite.get_instance().providers) def test_data_file_has_three_tests(self): clear() DATA_FILE('test_data.txt', name='new') test(data_provider='new')(valid_for_provider) start(listener=self._listener) self.assertEqual(3, TestSuite.tests_count()) def test_data_file_has_three_tests_with_long_path(self): clear() DATA_FILE('files/test_data.txt', name='new') test(data_provider='new')(valid_for_provider) start(listener=self._listener) self.assertEqual(3, TestSuite.tests_count()) def test_data_file_when_no_name(self): clear() DATA_FILE('files/test_data.txt') test(data_provider='files/test_data.txt')(valid_for_provider) start(listener=self._listener) self.assertEqual(3, TestSuite.tests_count()) def test_container_has_three_tests_default(self): clear() CONTAINER([_ for _ in range(3)]) test(data_provider='container')(valid_for_provider) start(listener=self._listener) self.assertEqual(3, TestSuite.tests_count()) def test_container_has_three_tests_generator(self): clear() CONTAINER((_ for _ in range(3))) test(data_provider='container')(valid_for_provider) start(listener=self._listener) self.assertEqual(3, TestSuite.tests_count()) def test_container_has_three_tests_str(self): clear() CONTAINER('123') test(data_provider='container')(valid_for_provider) start(listener=self._listener) self.assertEqual(3, TestSuite.tests_count()) def test_container_has_three_tests_with_name(self): clear() CONTAINER('123', name='text') test(data_provider='text')(valid_for_provider) start(listener=self._listener) self.assertEqual(3, TestSuite.tests_count()) def test_data_function_must_have_return_fail(self): def fail(): pass clear() with self.assertRaises(WrongDecoratedObject): provider(name='text')(fail)
class TestBeforeAndAfter(TestCase): _listener = Listener(0) def test_no_fixture_when_simple_test(self): clear() test(fn) start(listener=self._listener) self.assertEqual('test', common_str) def test_before_suite_default(self): clear() test(fn) before_suite(b_suite) start(listener=self._listener) self.assertEqual('bs_test', common_str) def test_after_suite_default(self): clear() test(fn) after_suite(a_suite) start(listener=self._listener) self.assertEqual('test_as', common_str) def test_before_group_default(self): clear() test(fn) before_group(b_group) start(listener=self._listener) self.assertEqual('bg_test', common_str) def test_after_group_default(self): clear() test(fn) after_group(a_group) start(listener=self._listener) self.assertEqual('test_ag', common_str) def test_before_test_default(self): clear() test(fn) before(b_test) start(listener=self._listener) self.assertEqual('bt_test', common_str) def test_after_test_default(self): clear() test(fn) after(a_test) start(listener=self._listener) self.assertEqual('test_at', common_str) def test_all_fixtures_default(self): clear() test(fn) after_group(a_group) before_group(b_group) before(b_test) after(a_test) before_suite(b_suite) after_suite(a_suite) start(listener=self._listener) self.assertEqual('bs_bg_bt_test_at_ag_as', common_str) def test_all_fixtures_default_with_two_tests(self): clear() test(fn) test(fn) after_group(a_group) before_group(b_group) before(b_test) after(a_test) before_suite(b_suite) after_suite(a_suite) start(listener=self._listener) self.assertEqual('bs_bg_bt_test_atbt_test_at_ag_as', common_str) def test_run_after_suite_if_before_failed_and_flag_true(self): clear() after_suite(always_run=True)(a_suite) test(fn) TestSuite.get_instance().is_before_failed = True start(listener=self._listener) self.assertEqual('_as', common_str) def test_not_run_after_suite_if_before_failed(self): clear() after_suite(a_suite) test(fn) TestSuite.get_instance().is_before_failed = True start(listener=self._listener) self.assertEqual('', common_str) def test_run_after_group_if_before_failed_and_flag_true(self): clear() after_group(always_run=True)(a_group) test(fn) list(TestSuite.get_instance().groups.values() )[0].is_before_failed = True start(listener=self._listener) self.assertEqual('_ag', common_str) def test_not_run_after_group_if_before_failed(self): clear() after_group(a_group) test(fn) list(TestSuite.get_instance().groups.values() )[0].is_before_failed = True start(listener=self._listener) self.assertEqual('', common_str) def test_no_after_test_if_before_failed(self): clear() test(fn) after_group(a_group) before_group(b_group) before(b_test) after(a_test) before_suite(b_suite) after_suite(a_suite) list(TestSuite.get_instance().groups.values() )[0].tests[0].is_before_failed = True start(listener=self._listener) self.assertEqual('bs_bg_bt__ag_as', common_str) def test_all_fixtures_default_with_two_tests_and_different_groups(self): clear() test(fn) test(groups=['api'])(fn) after_group(a_group) before_group(b_group) before(b_test) after(a_test) after_group(name='api')(a_group_2) before_group(name='api')(b_group_2) before(group_name='api')(b_test_2) after(group_name='api')(a_test_2) before_suite(b_suite) after_suite(a_suite) start(listener=self._listener) self.assertEqual('bs_bg_bt_test_at_agBG_BT_test_AT_AG_as', common_str) def test_retries_if_not_fails(self): clear() test(retries=3)(fn) start(listener=self._listener) self.assertEqual(1, len(TestSuite.get_instance().success())) self.assertEqual(1, TestSuite.get_instance().tests_count()) def test_retries_if_fails(self): clear() test(retries=3)(failed) start(listener=self._listener) self.assertEqual(0, len(TestSuite.get_instance().success())) self.assertEqual(3, len(TestSuite.get_instance().failed())) self.assertEqual(3, TestSuite.get_instance().tests_count()) def test_priorities(self): clear() test(priority=3)(b_suite) test(priority=2)(a_suite) test(priority=1)(b_group) test(fn) start(listener=self._listener) self.assertEqual('testbg__asbs_', common_str) def test_common_params(self): clear() test(par_1) test(par_2) start(listener=self._listener) self.assertEqual(runner.common.as_dict(), {'1': 1}) def test_no_tests_if_filter_not_existed_group(self): clear() test(par_1) test(groups=['a'])(par_2) start(listener=self._listener, groups=['b']) self.assertFalse(TestSuite.tests_count()) def test_one_test_if_filter_existed_group(self): clear() test(par_1) test(groups=['a'])(par_2) start(listener=self._listener, groups=['a']) self.assertEqual(1, TestSuite.tests_count()) def test_no_params_on_default(self): clear() test(fn) start(listener=self._listener) self.assertFalse(runner.common) def test_params_if_exists(self): clear() test(fn) start(listener=self._listener, params={'test': 'test'}) self.assertTrue(runner.common) self.assertEqual(runner.common.as_dict(), {'test': 'test'}) def test_params_if_exists_and_use_in_test(self): clear() test(par_1) start(listener=self._listener, params={'test': 'test'}) self.assertTrue(runner.common) self.assertEqual('test', runner.common['test']) self.assertEqual(1, runner.common['1']) def test_common_function_works(self): def name(): pass clear() common_function(name) test(failed) start(listener=self._listener) self.assertTrue(runner.common) self.assertEqual(name, runner.common.name) self.assertEqual(name, runner.common['name']) def test_common_function_works_with_params(self): def name(): pass clear() common_function(name) test(failed) start(listener=self._listener, params={'test': 'test'}) self.assertTrue(runner.common) self.assertEqual(name, runner.common.name) self.assertEqual('test', runner.common.test) self.assertEqual(2, len(runner.common.as_dict())) def test_common_function_raises_on_second_use_of_name(self): def name(): pass clear() common_function(name) with self.assertRaises(WrongDecoratedObject) as e: common_function(name) self.assertEqual(e.exception.args[0], 'Name "name" is already used at common object')
from unittest import main, TestCase import checking.runner as runner from checking.annotations import * from checking.classes.listeners.basic import Listener from tests.fixture_behaviour_test import clear a_list = [] _listener = Listener(0) _listener.on_failed = lambda t, e: a_list.append(e) class AssertTest(TestCase): def setUp(self) -> None: clear() a_list.clear() def test_assert_equal(self): def _(): assert 1 == 2 test(_) runner.start(listener=_listener) self.assertTrue(type(a_list[0]) is AssertionError) self.assertEqual(a_list[0].args[0], 'Objects are not equal (1 != 2)') def test_assert_not_equal(self): def _(): assert 1 != 1 test(_)
def _run(func): clear() test(func) start(listener=Listener(3))