コード例 #1
0
    def test_runner_uses_format(self):
        """Test that format is set on TAPTestResult FORMAT."""
        # Save the previous format in case **this** execution was using it.
        previous_format = TAPTestResult.FORMAT
        fmt = "{method_name}: {short_description}"

        TAPTestRunner.set_format(fmt)

        self.assertEqual(fmt, TAPTestResult.FORMAT)

        TAPTestResult.FORMAT = previous_format
コード例 #2
0
    def test_runner_uses_format(self):
        """Test that format is set on TAPTestResult FORMAT."""
        # Save the previous format in case **this** execution was using it.
        previous_format = TAPTestResult.FORMAT
        fmt = "{method_name}: {short_description}"

        TAPTestRunner.set_format(fmt)

        self.assertEqual(fmt, TAPTestResult.FORMAT)

        TAPTestResult.FORMAT = previous_format
コード例 #3
0
    def test_bad_format_string(self, fake_exit):
        """A bad format string exits the runner."""
        previous_format = TAPTestResult.FORMAT
        bad_format = "Not gonna work {sort_desc}"
        TAPTestRunner.set_format(bad_format)
        result = TAPTestResult(None, True, 1)
        test = mock.Mock()

        result._description(test)

        self.assertTrue(fake_exit.called)

        TAPTestResult.FORMAT = previous_format
コード例 #4
0
    def test_bad_format_string(self, fake_exit):
        """A bad format string exits the runner."""
        previous_format = TAPTestResult.FORMAT
        bad_format = "Not gonna work {sort_desc}"
        TAPTestRunner.set_format(bad_format)
        result = TAPTestResult(None, True, 1)
        test = mock.Mock()

        result._description(test)

        self.assertTrue(fake_exit.called)

        TAPTestResult.FORMAT = previous_format
コード例 #5
0
        setup.wait()
        if setup.returncode != 0:
            raise Exception('Setup failed')


def run_teardowns(tests):
    teardowns = []
    for test in tests:
        test_dir = path_from_name(test[0], 'plain')
        if os.path.isfile(os.path.join(test_dir, 'teardown.sh')):
            teardowns.append(
                subprocess.Popen('./teardown.sh',
                                 cwd=test_dir,
                                 stdout=subprocess.DEVNULL,
                                 stderr=subprocess.DEVNULL))
    for teardown in teardowns:
        teardown.wait()


if __name__ == '__main__':
    tests = get_test_cases()
    run_setups(tests)
    suite = unittest.TestSuite()
    for test in tests:
        suite.addTest(test[1]())
    runner = TAPTestRunner()
    runner.set_format('{method_name}')
    runner.set_stream(True)
    runner.run(suite)
    run_teardowns(tests)
コード例 #6
0
ファイル: run.py プロジェクト: danielshahaf/tappy
# 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)
コード例 #7
0
 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)
コード例 #8
0
                                           stderr=subprocess.DEVNULL))
    for setup in setups:
        setup.wait()
        if setup.returncode != 0:
            raise Exception('Setup failed')


def run_teardowns(tests):
    teardowns = []
    for test in tests:
        test_dir = path_from_name(test[0], 'plain')
        if os.path.isfile(os.path.join(test_dir, 'teardown.sh')):
            teardowns.append(subprocess.Popen('./teardown.sh', cwd=test_dir,
                                              stdout=subprocess.DEVNULL,
                                              stderr=subprocess.DEVNULL))
    for teardown in teardowns:
        teardown.wait()


if __name__ == '__main__':
    tests = get_test_cases()
    run_setups(tests)
    suite = unittest.TestSuite()
    for test in tests:
        suite.addTest(test[1]())
    runner = TAPTestRunner()
    runner.set_format('{method_name}')
    runner.set_stream(True)
    runner.run(suite)
    run_teardowns(tests)
コード例 #9
0
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