def startTest(self, test): """Override of the TextTestResult.startTest() method. The start of STDOUT and STDERR capture occurs here. """ # IO capture. if self.io_capture: # Store the original STDOUT and STDERR for restoring later on. self.orig_stdout = sys.stdout self.orig_stderr = sys.stderr # Catch stdout and stderr. self.capt = StringIO() sys.stdout = self.capt sys.stderr = self.capt # No capture. else: test_title(format_test_name(test.id()), desc=test.shortDescription(), width=status.text_width) # Place the test name in the status object. status.exec_lock.test_name = str(test) # Store the starting time. if self.timing_flag or not self.io_capture: self.time = time() # Execute the normal startTest method. super(RelaxTestResult, self).startTest(test)
def write_time(self, test_name): """Write the timing of the test to the stream. @param test_name: The TestCase name. @type test_name: str """ # Subtract the end time from the start time. self.time -= time() # Format the name. test_name = format_test_name(test_name) # The printout. if self.category == 'unit': time_str = '%7.2f ms' % (abs(self.time) * 1000) else: time_str = '%7.2f s' % abs(self.time) self.stream.write(' %s for %s\n' % (time_str, test_name))
def run(self, tests=None, runner=None, list_tests=False): """Run the system/functional tests. The system test list should be something like ['N_state_model.test_stereochem_analysis']. The first part is the imported test case class, the second is the specific test. @keyword tests: The list of system tests to preform. @type tests: list of str @keyword runner: A test runner such as TextTestRunner. For an example of how to write a test runner see the python documentation for TextTestRunner in the python source. @type runner: Test runner instance (TextTestRunner, BaseGUITestRunner subclass, etc.) @keyword list_tests: A flag which if True will cause the tests to be listed rather than executed. @type list_tests: bool """ # Create an array of test suites (add your new TestCase classes here). suite_array = [] # Specific tests. for test in tests: # The entire test class. if not search('\.', test): # Check that the class exists. if test not in globals(): raise RelaxError( "The system test class '%s' does not exist." % test) # The uninstantiated class object. obj = globals()[test] # Add the tests. suite_array.append(TestLoader().loadTestsFromTestCase(obj)) # Single system test. else: # Split. row = test.split('.') # Check. if len(row) != 2: raise RelaxError( "The test '%s' is not in the correct format. It should consist of the test case class, a dot, and the specific test." % test) # Unpack. class_name, test_name = row # Get the class object. obj = globals()[class_name] # Add the test. suite_array.append(TestLoader().loadTestsFromNames([test_name], obj)) # All tests. if not tests: suite_array.append( TestLoader().loadTestsFromTestCase(Align_tensor)) suite_array.append(TestLoader().loadTestsFromTestCase(Bmrb)) suite_array.append(TestLoader().loadTestsFromTestCase(Bruker)) suite_array.append(TestLoader().loadTestsFromTestCase(Angles)) suite_array.append( TestLoader().loadTestsFromTestCase(Chemical_shift)) suite_array.append(TestLoader().loadTestsFromTestCase(Ct)) suite_array.append(TestLoader().loadTestsFromTestCase(Dasha)) suite_array.append( TestLoader().loadTestsFromTestCase(Diffusion_tensor)) suite_array.append(TestLoader().loadTestsFromTestCase(Frame_order)) suite_array.append(TestLoader().loadTestsFromTestCase(Generic)) suite_array.append(TestLoader().loadTestsFromTestCase(Grace)) suite_array.append(TestLoader().loadTestsFromTestCase(Interatomic)) suite_array.append(TestLoader().loadTestsFromTestCase(Jw)) suite_array.append(TestLoader().loadTestsFromTestCase(Load_spins)) suite_array.append(TestLoader().loadTestsFromTestCase(Modelim)) suite_array.append(TestLoader().loadTestsFromTestCase(Mf)) suite_array.append(TestLoader().loadTestsFromTestCase(Modsel)) suite_array.append( TestLoader().loadTestsFromTestCase(Mol_res_spin)) suite_array.append( TestLoader().loadTestsFromTestCase(N_state_model)) suite_array.append(TestLoader().loadTestsFromTestCase(Noe)) suite_array.append( TestLoader().loadTestsFromTestCase(Noe_restraints)) suite_array.append(TestLoader().loadTestsFromTestCase(Palmer)) suite_array.append(TestLoader().loadTestsFromTestCase(Pcs)) suite_array.append(TestLoader().loadTestsFromTestCase(Peak_lists)) suite_array.append(TestLoader().loadTestsFromTestCase(Pipes)) suite_array.append(TestLoader().loadTestsFromTestCase(Rdc)) suite_array.append(TestLoader().loadTestsFromTestCase(Relax_data)) suite_array.append(TestLoader().loadTestsFromTestCase(Relax_disp)) suite_array.append(TestLoader().loadTestsFromTestCase(Relax_fit)) suite_array.append(TestLoader().loadTestsFromTestCase(Results)) suite_array.append(TestLoader().loadTestsFromTestCase(Selection)) suite_array.append(TestLoader().loadTestsFromTestCase(Sequence)) suite_array.append(TestLoader().loadTestsFromTestCase(Spectrum)) suite_array.append(TestLoader().loadTestsFromTestCase(State)) suite_array.append(TestLoader().loadTestsFromTestCase(Structure)) suite_array.append( TestLoader().loadTestsFromTestCase(Unit_vectors)) suite_array.append(TestLoader().loadTestsFromTestCase(Value)) # Group all tests together. full_suite = TestSuite(suite_array) # Only list the tests. if list_tests: for suite in full_suite._tests: for test in suite: print(format_test_name(test.id())) return True # Run the test suite. results = runner.run(full_suite) # Return the status of the tests. return results.wasSuccessful()
def run(self, tests=None, runner=None, list_tests=False): """Run the software verification tests. @keyword tests: The list of system tests to preform. @type tests: list of str @keyword runner: A test runner such as TextTestRunner. For an example of how to write a test runner see the python documentation for TextTestRunner in the python source. @type runner: Test runner instance (TextTestRunner, BaseGUITestRunner subclass, etc.) @keyword list_tests: A flag which if True will cause the tests to be listed rather than executed. @type list_tests: bool """ # Create an array of test suites (add your new TestCase classes here). suite_array = [] # Specific tests. for test in tests: # The entire test class. if not search('\.', test): # Check that the class exists. if test not in globals(): raise RelaxError( "The system test class '%s' does not exist." % test) # The uninstantiated class object. obj = globals()[test] # Add the tests. suite_array.append(TestLoader().loadTestsFromTestCase(obj)) # Single system test. else: # Split. row = test.split('.') # Check. if len(row) != 2: raise RelaxError( "The test '%s' is not in the correct format. It should consist of the test case class, a dot, and the specific test." % test) # Unpack. class_name, test_name = row # Get the class object. obj = globals()[class_name] # Add the test. suite_array.append(TestLoader().loadTestsFromNames([test_name], obj)) # All tests. if not tests: suite_array.append(TestLoader().loadTestsFromTestCase(Library)) suite_array.append( TestLoader().loadTestsFromTestCase(Status_object)) # Group all tests together. full_suite = TestSuite(suite_array) # Only list the tests. if list_tests: for suite in full_suite._tests: for test in suite: print(format_test_name(test.id())) return True # Run the test suite. results = runner.run(full_suite) # Return the status of the tests. return results.wasSuccessful()
def run(self, tests=None, runner=None, list_tests=False): """Run a unit test or set of unit tests. @keyword tests: The list of system tests to perform. @type tests: list of str @keyword runner: A unit test runner such as TextTestRunner. None indicates use of the default unit test runner. For an example of how to write a test runner see the python documentation for TextTestRunner in the python source. @type runner: Unit test runner instance (TextTestRunner, BaseGUITestRunner subclass, etc.) @keyword list_tests: A flag which if True will cause the tests to be listed rather than executed. @type list_tests: bool @return: A string indicating success or failure of the unit tests run. @rtype: str """ msg = "Either set self.%s to a %s directory or set search_for_%s_path in self.__init__ to True" if self.unit_test_directory == None: raise Exception(msg % ('unit_test_directory', 'unit test', 'unit_test')) if self.system_directory == None: raise Exception(msg % ('system_directory', 'system', 'root')) # Title printout. if self.verbose: print('\nTesting units...') print('----------------') print('') # The test module path for individual tests. if tests: # Initialise the list of paths. module_paths = [] # Loop over each test. for test in tests: # Strip out the leading 'test_suite.unit_tests.' if present. test = test.replace('test_suite.unit_tests.', '') test = test.replace( 'test_suite%sunit_tests%s' % (os.sep, os.sep), '') # Replace the Python '.' separator with the system's path separator. test = test.replace('.', os.sep) # Append to the module path list. module_paths += self.paths_from_test_module(self.test_module + os.sep + test) # The test module path for all tests. else: module_paths = self.paths_from_test_module(self.test_module) if self.verbose: print("%-22s %s" % ('Test module:', self.test_module)) print("%-22s %s" % ('Root path', self.root_path)) print("%-22s %s" % ('System directory:', self.system_directory)) print("%-22s %s" % ('Unit test directory:', self.unit_test_directory)) for i, elem in enumerate(module_paths): print("%-22s %s" % ('Module path %d:' % i, elem)) print('') # add SystemDirectory to python path sys.path.pop(0) sys.path.insert(0, self.system_directory) tests = None # Load all tests from a directory. for module_path in module_paths: module_string = os.path.join(*module_path) if os.path.isdir(module_string): #iterate and load unit tests from module path finder = Test_finder(module_string, self.test_case_patterns) finder.scan_paths() tests = finder.suite break # Execute specific tests. if tests == None: for module_tuple in module_paths: # The package path. package_path = module_tuple[0] for i in range(len(module_tuple) - 2): package_path = os.path.join(package_path, module_tuple[i]) # The module name. module_name = module_tuple[-2] # The class name. class_name = module_tuple[-1] # Load the tests. tests = load_test_case(package_path, module_name, class_name) # Only list the tests. if list_tests: for suite in tests: for test in suite: print(format_test_name(test.id())) return True if runner == None: runner = unittest.TextTestRunner() if self.verbose: print('Results') print('-------') print('') # Run the unit tests and catch the TestResult object. if tests != None and tests.countTestCases() != 0: # Run the test. results = runner.run(tests) result_string = results.wasSuccessful() elif tests == None: results = None result_string = 'Error: no test directories found for input module: %s' % self.test_module print(result_string) else: results = None result_string = 'Note: no tests found for input module: %s' % self.test_module print(result_string) # Return the result of all the tests. return result_string