def test_multiple_consumers(testdir, redis_connection, redis_args): """Pull tests from multiple test runs simultaneously.""" test_file_name = "test_multiple_consumers.py" utils.create_test_file(testdir, test_file_name, """ def test_multiple_consumers(): assert True """) for i in range(100): redis_connection.lpush(redis_args['redis-list-key'], test_file_name + "::test_multiple_consumers") py_test_args = utils.get_standard_args(redis_args) processes = setup_multiple_consumer_processes(testdir, py_test_args, 5) for proc in processes: proc[0].start() for proc in processes: proc[0].join() exit_result = proc[1].recv()[0] assert exit_result == EXIT_OK assert redis_connection.llen(redis_args['redis-list-key']) == 0
def test_create_file(self): self.assertEqual(self.file.account, self.account.id) new_file = utils.create_test_file(self.account, file_data='test data1', overwrite=False) self.assertNotEqual(self.file.name, new_file.name) new_file = utils.create_test_file(self.account, file_data='test data2', file_name=self.file.name, overwrite=True) self.assertEqual(self.file.name, new_file.name)
def test_no_consumption_of_item(testdir, redis_args): """Make sure that we don't run tests when the list is empty.""" test_file_name = "test_not_used" utils.create_test_file(testdir, test_file_name, """ def test_run_should_run(): assert True """) py_test_args = utils.get_standard_args(redis_args) result = testdir.runpytest(*py_test_args) assert result.ret == EXIT_OK
def test_conf_tests(testdir, redis_connection, redis_args): """Ensure that conftest.py are executed.""" test_file_name = "test_conf_test.py" fixture_prints = [ "fixture_1_ran", "fixture_2_ran" ] num_times_to_run_tests = 3 utils.create_test_file(testdir, test_file_name, """ def test_run0(a_fixture): assert not a_fixture def test_run1(another_fixture): assert another_fixture def test_run2(): assert False """) test_file_name_2 = "conftest.py" utils.create_test_file(testdir, test_file_name_2, """ import pytest def pytest_collection_modifyitems(session, config, items): if "test_run2" in items[0].nodeid: items[:] = [] @pytest.fixture def a_fixture(): print print "{}" return False @pytest.fixture def another_fixture(): print print "{}" return True """.format(*fixture_prints)) for i in range(num_times_to_run_tests): redis_connection.lpush(redis_args['redis-list-key'], test_file_name) py_test_args = utils.get_standard_args(redis_args) + ["-s"] result = testdir.runpytest(*py_test_args) assert result.ret == EXIT_TESTSFAILED result.stdout.fnmatch_lines([test for test in fixture_prints] * num_times_to_run_tests)
def create_test_file(testdir): """Create test file and return array of paths to tests.""" test_filename = "test_file.py" test_filename_contents = """ def test_exists(): assert True def test_does_exist(): assert True """ utils.create_test_file(testdir, test_filename, test_filename_contents) return [test_filename + "::test_exists", test_filename + "::test_does_exist"]
def test_after_timestamp(self): acc = self.account test_file_4 = utils.create_test_file(acc) after = test_file_4.modified test_file_5 = utils.create_test_file(acc) time.sleep(2) results = acc.recent.all(after=after) self.assertNotIn(test_file_4.id, [r.id for r in results]) self.assertGreaterEqual(results[0].modified, after) results = acc.recent.all(after=test_file_5.modified) self.assertNotIn(test_file_5.id, [r.id for r in results])
def test_non_existent_test_name(testdir, redis_connection, redis_args): """Entire test should fail if a non-existent test is specfied.""" test_file_name = "test_name.py" utils.create_test_file(testdir, test_file_name, """ def test_name_dne(): assert True """) redis_connection.lpush(redis_args['redis-list-key'], test_file_name + "::test_wrong_name") py_test_args = utils.get_standard_args(redis_args) result = testdir.runpytest(*py_test_args) assert result.ret == EXIT_USAGEERROR
def test_mult_results_search(self): acc = self.account test_file_name = 'search' + str(random.random())[2:] + '.txt' root_folder = utils.create_or_get_test_folder(acc) test_folder_1 = acc.folders.create(data={ 'parent_id': root_folder.id, 'name': 'folder %s' % random.randint(0, 10e8)}) test_folder_2 = acc.folders.create(data={ 'parent_id': root_folder.id, 'name': 'folder %s' % random.randint(0, 10e8)}) test_file_1 = utils.create_test_file(acc, folder=test_folder_1, file_name=test_file_name) test_file_2 = utils.create_test_file(acc, folder=test_folder_2, file_name=test_file_name) if acc.service == 'box': time.sleep(210) results = acc.search.all(q=test_file_name) self.assertEqual({results[0].id, results[1].id}, {test_file_1.id, test_file_2.id})
def test_simple_search(self): acc = self.account test_file_name = 'search' + str(random.random())[2:] + '.txt' test_file = utils.create_test_file(acc, file_name=test_file_name) if acc.service == 'box': time.sleep(210) results = acc.search.all(q=test_file_name) self.assertGreater(results, 0) if results: self.assertEqual(results[0].id, test_file.id)
def test_simple_recent_page(self): acc = self.account test_file_3 = utils.create_test_file(acc) time.sleep(0.5) results = acc.recent.all(page_size=1, page=1) self.assertEqual(len(results), 1) # Subject to race conditions. results = acc.recent.all() self.assertIn(test_file_3.id, [r.id for r in results])
def test_external_arguments(testdir, redis_connection, redis_args): """Ensure that the plugin doesn't intefere with other plugins.""" test_file_name = "test_external_arguments.py" utils.create_test_file(testdir, test_file_name, """ def test_run_should_run(): assert True """) redis_connection.lpush(redis_args['redis-list-key'], test_file_name + "::test_run_should_run") junitxml_filename = "pytest.xml" py_test_args = utils.get_standard_args(redis_args) + \ ['--junitxml=' + junitxml_filename] junitxml_path = str(testdir.tmpdir) + "/" + junitxml_filename result = testdir.runpytest(*py_test_args) assert os.path.exists(junitxml_path) assert result.ret == EXIT_OK
def test_module_test_name(testdir, redis_connection, redis_args): """Test path as a module name.""" module_1_name = "test_module_1" module_1_test_filename = "test_module_1_file.py" module_1_test_filename_contents = """ def test_exists(): assert True def test_does_exist(): assert True def test_random_test(): assert True """ module_2_name = "test_module_2" module_2_test_filename = "test_module_2_file.py" module_2_test_filename_contents = """ def test_exists(): assert True def test_does_exist_2(): assert True def test_random_test_2(): assert True """ utils.create_test_dir(testdir, module_1_name) utils.create_test_file(testdir, module_1_name + "/" + module_1_test_filename, module_1_test_filename_contents) utils.create_test_dir(testdir, module_2_name) utils.create_test_file(testdir, module_2_name + "/" + module_2_test_filename, module_2_test_filename_contents) py_test_args = utils.get_standard_args(redis_args) for module in [module_1_name, module_2_name]: redis_connection.lpush(redis_args['redis-list-key'], module) result = testdir.runpytest(*py_test_args) result.stdout.fnmatch_lines([ "*" + module + "*PASSED", "*" + module + "*PASSED" ]) assert result.ret == EXIT_OK
def get_elaborate_test_directories_and_paths(testdir, folders): """Create an elaborate depth=1 directory structure.""" num_test_methods_per_file = 2 # Create the contents of the test files for folder in folders.keys(): utils.create_test_dir(testdir, folder) for a_file in folders[folder]: file_path = folder + "/" + a_file file_text = "" for test_num in range(num_test_methods_per_file): test_name = "test_multiple_consumers_{}_{}".format( folder + "_" + a_file.split(".")[0], test_num) file_text += """ def {}(): assert True """.format(test_name) # We push each test to the queue # `num_times_to_run_tests` times to # have a bit more work to do test_name_path = folder + "/" + a_file + "::" + test_name yield test_name_path utils.create_test_file(testdir, file_path, file_text)
def test_complex_recent_page(self): acc = self.account files = {} for i in xrange(1, 6): files['test_file_{0}'.format(i)] = utils.create_test_file(acc) time.sleep(0.5) results = acc.recent.all(page_size=3, page=1) self.assertEqual(len(results), 3) # Subject to race conditions self.assertEqual(results[0].id, files['test_file_5'].id) self.assertEqual(results[1].id, files['test_file_4'].id) self.assertEqual(results[2].id, files['test_file_3'].id) results = acc.recent.all(page_size=1, page=5) self.assertEqual(results[0].id, files['test_file_1'].id)
def setUp(self): acc = self.account if acc.service in list_permissions: self.test_folder = utils.create_or_get_test_folder(acc) self.test_file = utils.create_test_file(acc) new_roles = { "*****@*****.**": "reader", "*****@*****.**": "writer" } if acc.service in change_folder_permissions: self.new_roles = new_roles self.test_folder.permissions.create(data=self.new_roles) if acc.service in change_file_permissions: self.new_roles = new_roles self.test_file.permissions.create(data=self.new_roles)
def test_run_once(testdir, redis_connection, redis_args): """Ensure that collected tests are only executed a single time.""" module_name = "test_run_once_module" module_test_filename = "test_run_once.py" module_test_filename_contents = """ import local def test_exists(): assert True def test_does_exist(): assert True def test_random_test(): assert True """ utils.create_test_dir(testdir, module_name) utils.create_test_file(testdir, module_name + "/" + module_test_filename, module_test_filename_contents) utils.create_test_file(testdir, module_name + "/__init__.py", "") utils.create_test_file(testdir, module_name + "/local.py", """ print "Imported" """) py_test_args = utils.get_standard_args(redis_args) + ["-s"] redis_connection.lpush(redis_args['redis-list-key'], module_name + "/" + module_test_filename + "::test_exists") redis_connection.lpush(redis_args['redis-list-key'], module_name + "/" + module_test_filename + "::test_does_exist") redis_connection.lpush(redis_args['redis-list-key'], module_name + "/" + module_test_filename + "::test_random_test") # We've pushed 3 tests from the same file, # we should import local.py only once result = testdir.runpytest(*py_test_args) result.stdout.fnmatch_lines([ "*Imported*", "*test_exists PASSED", "*test_does_exist PASSED", "*test_random_test PASSED" ]) assert result.ret == EXIT_OK
def setUp(self): self.test_file = utils.create_test_file(self.account) self.link = self.account.links.create(data={'file_id': self.test_file.id})
def setUp(self): self.test_file = utils.create_test_file(self.account) self.link = self.account.links.create( data={'file_id': self.test_file.id})
def setUp(self): self.cursor = self.get_latest_cursor() self.file = utils.create_test_file(self.account, folder=self.test_folder)
def setUp(self): acc = self.account self.test_file_1 = utils.create_test_file(acc) self.test_file_2 = utils.create_test_file(acc)
def main(argv): tests = utils.load_tests(argv[1]) test_file_name = utils.create_test_file(argv[2], tests) utils.run_tests(test_file_name) os.remove(test_file_name)
def setUp(self): self.folder = utils.create_or_get_test_folder(self.account) self.file = utils.create_test_file(self.account)