def test_runner_uses_outdir(self): """Test that the test runner sets the TAPTestResult OUTDIR so that TAP files will be written to that location. Setting class attributes to get the right behavior is a dirty hack, but the unittest classes aren't very extensible. """ # Save the previous outdir in case **this** execution was using it. previous_outdir = TAPTestResult outdir = tempfile.mkdtemp() TAPTestRunner.set_outdir(outdir) self.assertEqual(outdir, TAPTestResult.OUTDIR) TAPTestResult.OUTDIR = previous_outdir
def test_runner_uses_outdir(self): """Test that the test runner sets the outdir so that TAP files will be written to that location. Setting class attributes to get the right behavior is a dirty hack, but the unittest classes aren't very extensible. """ # Save the previous outdir in case **this** execution was using it. previous_outdir = _tracker.outdir outdir = tempfile.mkdtemp() TAPTestRunner.set_outdir(outdir) self.assertEqual(outdir, _tracker.outdir) _tracker.outdir = previous_outdir
# Copyright (c) 2015, Matt Layman import os import unittest from tap import TAPTestRunner if __name__ == '__main__': tests_dir = os.path.dirname(os.path.abspath(__file__)) loader = unittest.TestLoader() tests = loader.discover(tests_dir) runner = TAPTestRunner() runner.set_outdir('testout') runner.set_format('Hi: {method_name} - {short_description}') runner.run(tests)
# -*- coding: utf-8; -*- # # Copyright © 2014, Nicolas CANIART <*****@*****.**> # import os import sys from unittest import TestLoader try: from tap import TAPTestRunner as TestRunner tap_output = os.path.join(os.path.dirname(__file__), 'tap') TestRunner.set_outdir(tap_output) except ImportError: from unittest import TextTestRunner as TestRunner path = os.path.dirname(__file__) res = TestRunner(verbosity=2).run( TestLoader().discover('./', pattern='test_*.py')) sys.exit(not res.wasSuccessful()) # vim: syntax=python:sws=4:sw=4:et:
def execute(self): runner = TAPTestRunner() runner.set_outdir('logs/TAP/') runner.set_format('{method_name} and {short_description}') runner.set_combined(True) runner.run(self.suite)
# Copyright (c) 2018, Matt Layman and contributors import os import unittest from tap import TAPTestRunner if __name__ == "__main__": tests_dir = os.path.dirname(os.path.abspath(__file__)) loader = unittest.TestLoader() tests = loader.discover(tests_dir) runner = TAPTestRunner() runner.set_outdir("testout") runner.set_format("Hi: {method_name} - {short_description}") runner.run(tests)
# -*- coding: utf-8; -*- # # Copyright © 2014-2015, Nicolas CANIART <*****@*****.**> # import os import sys from unittest import TestLoader path = os.path.dirname(__file__) try: from tap import TAPTestRunner as TestRunner TestRunner.set_outdir(os.path.join(path, 'tap')) except ImportError: from unittest import TextTestRunner as TestRunner res = TestRunner(verbosity=2).run( TestLoader().discover('./', pattern='test_*.py')) sys.exit(not res.wasSuccessful()) # vim: syntax=python:sws=4:sw=4:et:
# -*- coding: utf-8; -*- # # Copyright © 2014-2015, Nicolas CANIART <*****@*****.**> # import os import sys from unittest import TestLoader path = os.path.dirname(__file__) try: from tap import TAPTestRunner as TestRunner TestRunner.set_outdir(os.path.join(path, 'tap')) except ImportError: from unittest import TextTestRunner as TestRunner res = TestRunner(verbosity=2).run(TestLoader().discover('./', pattern='test_*.py')) sys.exit(not res.wasSuccessful()) # vim: syntax=python:sws=4:sw=4:et:
class UnitTestExecutor(Executor): """ Executor implementation to run the unit tests of a component. """ def __init__(self, component: str, application: SdeApplication) -> None: """ Creates a new UnitTestExecutor instance. Args: component: The component which will be tested. application: The SDE application instance. measure_coverage: Whether or not unit test coverage should be measured. """ super().__init__(application) self._test_runner = None """ The test runner used to execute the test. """ self._component = component """ The component to test. """ def execute(self) -> None: """ Executes the tests. """ logger = logging.getLogger('suisei.sde') # Prepare the test directory self._prepare_directory() components = [] if self._component != 'all': components.append(self._component) else: components = self._application.Configuration.Components for component in components: logger.debug('Executing unit tests for component %s', component) self._create_test_runner(component) test_suite = self._build_test_suite(component) self._test_runner.run(test_suite) logger.debug('All tests executed.') @staticmethod def _prepare_directory() -> None: """ Prepare the .testfiles directory. Create it if it doesn't exist and remove all files from it if it already exists. """ test_directory = os.path.abspath('./.testfiles/') if os.path.isdir(test_directory): # There is already a .testfiles directory, clean it up import shutil shutil.rmtree(test_directory) # Create an empty .testfiles directory os.makedirs(test_directory) def _create_test_runner(self, component: str) -> None: """ Creates the test runner to use for test execution. Args: component: Name of the component to test. """ logger = logging.getLogger('suisei.sde') logger.debug('Creating test runner for component %s...', component) try: from tap import TAPTestRunner self._test_runner = TAPTestRunner( verbosity=self._application.Configuration.get_ut_verbosity( component)) self._test_runner.set_outdir( self._application.Configuration.get_ut_outdir(component)) self._test_runner.set_format( self._application.Configuration.get_ut_log_format(component)) except ImportError: logger.warning('The TAP test runner is not installed on the host ' 'system, falling back to the default unit test ' 'runner.') self._test_runner = unittest.TextTestRunner( verbosity=self._application.Configuration.get_ut_verbosity( component)) logger.debug('Test runner created successfully.') def _build_test_suite(self, component: str) -> unittest.TestSuite: """ Builds the test suite for the given component. If test coverage is not measured, then we can just use Python's built-in test discovery tool to gather the tests to run. When test coverage is measured all tests need to be imported manually here to avoid coverage missing imports and function definitions. Args: component: Name of the component to test. """ logger = logging.getLogger('suisei.sde') logger.debug('Building test suite to execute...') test_loader = unittest.TestLoader() unit_test_path = \ self._application.Configuration.get_test_file_path(component) logger.debug('Path to search for test files: %s', unit_test_path) logger.debug('Using Python\'s internal test discovery...') test_suite = test_loader.discover(start_dir=unit_test_path, pattern='test_*.py') return test_suite
# Copyright (c) 2019, Matt Layman and contributors import os import unittest from tap import TAPTestRunner if __name__ == "__main__": tests_dir = os.path.dirname(os.path.abspath(__file__)) loader = unittest.TestLoader() tests = loader.discover(tests_dir) runner = TAPTestRunner() runner.set_outdir("testout") runner.set_format("Hi: {method_name} - {short_description}") runner.run(tests)