def test_one_pattern_not_found(self): ok_pattern = os.path.join(self.root, "file1.py:TestClass001") loader = Loader() unused = loader.get_runnables([ok_pattern]) for pattern in [ "file1.py:NonexistentTest", "file1.py:TestClass001.nonexistent_method", "file1.py:TestClass001(param=1)().test_method_1", "file1.py:TestClass001()(param=1).test_method_1", "file1.py:TestClass001()().test_method_1(param=1)", ]: pattern = os.path.join(self.root, pattern) with self.assertRaises(CannotLoadTests): Loader().get_runnables([ok_pattern, pattern])
def get_tests_from_dirs(dirs): tests = {} with slash.Session(): runnables = Loader().get_runnables(dirs) for r in runnables: rel_path = os.path.relpath(r.__slash__.address, '.') full_name = rel_path.split('\\')[-1] if not ('(' in full_name): full_name += '()' p = re.compile(r'(.*):(.*)(\(.*\))') items = p.findall(full_name) filename = items[0][0] filepath = os.path.dirname(rel_path) + '\\' + filename testname = items[0][1] opt = items[0][2] if filepath in tests: if testname in tests[filepath]: tests[filepath][testname] += ' ' + opt else: tests[filepath][testname] = opt else: tests[filepath] = {testname: opt} return tests
def test_total_num_tests(suite): suite.debug_info = False path = suite.commit() with Session() as s: Loader().get_runnables(path) assert s.get_total_num_tests() == len(suite)
def test_import_errors_with_session(self): with Session() as s: tests = list(Loader().iter_path(self.root)) self.assertTrue(tests) self.assertFalse(s.result.global_result.is_success()) errors = s.result.global_result.get_errors() self._assert_file_failed_with(errors, "test_3.py", SyntaxError) self._assert_file_failed_with(errors, "test_4.py", ImportError)
def test_setting_getting_tags_on_metadata(taggable): slash.tag('tagname')(taggable) with slash.Session() as s: # pylint: disable=unused-variable tests = Loader().get_runnables(taggable) assert tests for t in tests: assert 'tagname' in t.__slash__.tags
def test_loader_skips_empty_dirs(tmpdir): tests_dir = tmpdir.join('tests') with tests_dir.join('.dir').join('test_something.py').open( 'w', ensure=True) as f: f.write('def test_something():\n pass') with Session(): runnables = Loader().get_runnables(str(tests_dir)) assert runnables == []
def _test_iter_package(self, import_first, force_init_py): sys.path.insert(0, self.root) package_name = "{0}.tests".format(self.package_name) if import_first: __import__(self.package_name) filename = sys.modules[self.package_name].__file__ if force_init_py and not filename.endswith("__init__.py") and not filename.endswith("__init__.pyc"): sys.modules[self.package_name].__file__ = os.path.join(filename, "__init__.py") self.assert_all_discovered(Loader().iter_package(self.package_name)) self.assertIn(self.package_name, sys.modules)
def test_timeout_no_request_to_server(config_override, runnable_test_dir): config_override("parallel.no_request_timeout", 1) with Session(): runnables = Loader().get_runnables(str(runnable_test_dir)) parallel_manager = ParallelManager([]) parallel_manager.start_server_in_thread(runnables) parallel_manager.server.state = ServerStates.SERVE_TESTS with slash.assert_raises(ParallelTimeout) as caught: parallel_manager.start() assert 'No request sent to server' in caught.exception.args[0]
def test_children_not_connected_timeout(runnable_test_dir, config_override): config_override("parallel.worker_connect_timeout", 0) config_override("parallel.num_workers", 1) with Session(): runnables = Loader().get_runnables(str(runnable_test_dir)) parallel_manager = ParallelManager([]) parallel_manager.start_server_in_thread(runnables) time.sleep(0.1) with slash.assert_raises(ParallelTimeout) as caught: parallel_manager.wait_all_workers_to_connect() assert caught.exception.args[0] == 'Not all clients connected'
def test_loader_warns_duplicate_test_funcs(tmpdir): tests_dir = tmpdir.join('tests') full_path = tests_dir.join('.dir').join('test_something.py') test_name = 'test_something' with full_path.open('w', ensure=True) as f: f.write('def {0}():\n assert True\n'.format(test_name)) f.write('def {0}():\n assert True\n'.format(test_name)) with Session() as session: Loader().get_runnables([str(full_path)]) assert len(session.warnings) == 1 assert 'Duplicate' in session.warnings.warnings[0].details['message'] assert test_name in session.warnings.warnings[0].details['message']
def test_error_cleanups(self): class Test(slash.Test): def test(self_): slash.add_cleanup(self.events.cleanup, 1) slash.add_cleanup(self.events.cleanup, 2) raise Exception("!!!") self.events.cleanup(2).and_raise(SecondException()) self.events.cleanup(1).and_raise(FirstException()) self.forge.replay() with Session() as session: slash.runner.run_tests(Loader().iter_test_factory(Test)) [result] = session.result.iter_test_results() [err1, err2, err3] = result.get_errors()
def test_loader_sort_filenames(tmpdir): tests_dir = tmpdir.join(str(uuid4())) filenames = [] for _ in range(10): filename = str(uuid4()).replace('-', '') + '.py' with tests_dir.join(filename).open('w', ensure=True) as f: f.write('def test_something():\n pass') filenames.append(filename) with Session(): runnables = Loader().get_runnables(str(tests_dir)) assert [ os.path.basename(runnable.__slash__.file_path) for runnable in runnables ] == sorted(filenames)
def test_cleanups_within_cleanups_preserve_scope(checkpoint1): """Cleanups added from within other cleanups should happen within the scope of the parent cleanups """ @slash.parametrize('x', [1, 2]) def test_something(x): pass with slash.Session() as s: [fake_test1, fake_test2] = Loader().get_runnables(test_something) # pylint: disable=unbalanced-tuple-unpacking s.scope_manager.begin_test(fake_test1) def cleanup(): slash.add_cleanup(checkpoint1) slash.add_cleanup(cleanup) assert not checkpoint1.called s.scope_manager.end_test(fake_test1) assert checkpoint1.called
def test_cleanups(self): class Test(slash.Test): def test1(self_): self.events.test1() slash.add_cleanup(self.events.cleanup, "test1 cleanup 2") slash.add_cleanup(self.events.cleanup, "test1 cleanup 1") def test2(self_): self.events.test2() slash.add_cleanup(self.events.cleanup, "test2 cleanup") with self.forge.any_order(): with self.forge.ordered(): self.events.test1() self.events.cleanup("test1 cleanup 1") self.events.cleanup("test1 cleanup 2") with self.forge.ordered(): self.events.test2() self.events.cleanup("test2 cleanup") self.forge.replay() with Session(): slash.runner.run_tests(Loader().iter_test_factory(Test))
def test_loader(): return Loader()
def test_iter_path(self): self.assert_all_discovered(Loader().iter_path(self.root))
def slash_runner_thread(test_files): #slash_run(args) with slash.Session() as session: tests = Loader().get_runnables(test_files) with session.get_started_context(): slash.run_tests(tests)
def test_import_errors_without_session(self): with self.assertRaises((SyntaxError, ImportError)): list(Loader().iter_path(self.root))