Beispiel #1
0
 def test_formatDifferState(self):
     obj = BasicFormatter()
     with mock.patch(
             'moosetools.moosetest.formatters.BasicFormatter._formatResult'
     ) as fm:
         obj.formatDifferResult(name='Andrew')
     fm.assert_called_once_with(indent=' ' * 4, name='Andrew')
Beispiel #2
0
    def test_formatComplete(self):
        obj = BasicFormatter()

        class TestCaseProxy(object):
            def __init__(self, name, state, t):
                self._name = name
                self.state = state
                self.time = t

            def name(self):
                return self._name

        complete = [
            TestCaseProxy('A', TestCase.Result.PASS, 10),
            TestCaseProxy('B', TestCase.Result.FATAL, 20)
        ]
        with mock.patch('moosetools.mooseutils.color_text',
                        side_effect=lambda *args: args[0]):
            text = obj.formatComplete(complete)
        self.assertIn("Executed 2 tests", text)
        self.assertNotIn("in 40.0 seconds", text)
        self.assertIn(
            "OK:1 SKIP:0 TIMEOUT:0 DIFF:0 ERROR:0 EXCEPTION:0 FATAL:1", text)

        kwargs = {'duration': 40}
        with mock.patch('moosetools.mooseutils.color_text',
                        side_effect=lambda *args: args[0]):
            text = obj.formatComplete(complete, **kwargs)

        self.assertIn("Executed 2 tests in 40.0 seconds", text)
        self.assertIn(
            "OK:1 SKIP:0 TIMEOUT:0 DIFF:0 ERROR:0 EXCEPTION:0 FATAL:1", text)

        self.assertIn('Longest running test(s)', text)
        self.assertIn('\n  20.00s B\n  10.00s A', text)
Beispiel #3
0
 def test_formatRunnerResult(self):
     obj = BasicFormatter()
     with mock.patch(
             'moosetools.moosetest.formatters.BasicFormatter._formatResult'
     ) as fm:
         obj.formatRunnerResult(name='Andrew')
     fm.assert_called_once_with(name='Andrew')
Beispiel #4
0
 def test_formatDifferState(self):
     obj = BasicFormatter()
     with mock.patch(
             'moosetools.moosetest.formatters.BasicFormatter._formatState'
     ) as fm:
         obj.formatDifferState(name='Andrew', percent=42, duration=42)
     fm.assert_called_once_with(indent=' ' * 4, name='Andrew')
Beispiel #5
0
    def testDefault(self):
        obj = BasicFormatter()
        self.assertIsInstance(obj._max_state_width, int)
        self.assertTrue(obj._max_state_width > 0)

        self.assertIsInstance(obj._extra_width, int)
        self.assertTrue(obj._extra_width > 0)
Beispiel #6
0
    def test_formatState(self):
        obj = BasicFormatter(width=60)

        kwargs = dict()
        kwargs['percent'] = 42
        kwargs['duration'] = 123.4
        kwargs['state'] = TestCase.Progress.RUNNING
        kwargs['name'] = "The:name/of/test"
        kwargs['reasons'] = None

        with mock.patch('moosetools.mooseutils.color_text',
                        side_effect=lambda *args: args[0]):
            text = obj._formatState(**kwargs)
        self.assertEqual(
            text,
            "The:name/of/test...................RUNNING    42% [123.4s]  ")

        kwargs['reasons'] = ['reason']
        with mock.patch('moosetools.mooseutils.color_text',
                        side_effect=lambda *args: args[0]):
            text = obj._formatState(**kwargs)
        self.assertEqual(
            text,
            "The:name/of/test..........[reason] RUNNING    42% [123.4s]  ")

        # Long reasons
        kwargs['reasons'] = ['the', 'reasons', 'are', 'many']
        with mock.patch('moosetools.mooseutils.color_text',
                        side_effect=lambda *args: args[0]):
            text = obj._formatState(**kwargs)
        self.assertEqual(
            text,
            "The:name/of/test..[...; are; many] RUNNING    42% [123.4s]  ")

        # Long name
        kwargs[
            'name'] = "This/is/a/long/name/that/will/get/shortened/in/the/middle"
        kwargs['reasons'] = None
        with mock.patch('moosetools.mooseutils.color_text',
                        side_effect=lambda *args: args[0]):
            text = obj._formatState(**kwargs)
        self.assertEqual(
            text,
            "This/is/a/l.../the/middle..........RUNNING    42% [123.4s]  ")

        # Long name and long reasons
        kwargs[
            'name'] = "This/is/a/long/name/that/will/get/shortened/in/the/middle"
        kwargs['reasons'] = ['the', 'reasons', 'are', 'many']
        with mock.patch('moosetools.mooseutils.color_text',
                        side_effect=lambda *args: args[0]):
            text = obj._formatState(**kwargs)
        self.assertEqual(
            text,
            "This/is/a/l.../the/middle..[...ny] RUNNING    42% [123.4s]  ")
Beispiel #7
0
    def testWidth(self):
        obj = BasicFormatter(width=42)
        self.assertEqual(obj.width(), 42)

        with mock.patch('shutil.get_terminal_size', return_value=(1980, None)):
            obj = BasicFormatter()
            self.assertEqual(obj.width(), 1980)
Beispiel #8
0
    def test_formatResult(self):
        obj = BasicFormatter(width=60)

        kwargs = dict()
        kwargs['percent'] = 42
        kwargs['duration'] = 123.4
        kwargs['state'] = TestCase.Result.PASS
        kwargs['name'] = "The:name/of/test"
        kwargs['reasons'] = None
        kwargs['stdout'] = 'regular output\n'
        kwargs['stderr'] = 'error output\n'

        with mock.patch('moosetools.mooseutils.color_text',
                        side_effect=lambda *args: args[0]):
            text = obj._formatResult(**kwargs)
        self.assertEqual(text, None)

        obj = BasicFormatter(width=60, print_state=TestCase.Result.PASS)
        with mock.patch('moosetools.mooseutils.color_text',
                        side_effect=lambda *args: args[0]):
            text = obj._formatResult(**kwargs)
        self.assertIn('regular output', text)
        self.assertIn('error output', text)
Beispiel #9
0
 def testShortenLine(self):
     obj = BasicFormatter()
     self.assertEqual(obj.shortenLine("andrew", 2), "a...w")
Beispiel #10
0
 def testShortenLines(self):
     obj = BasicFormatter(max_lines=2)
     self.assertEqual(obj.shortenLines("one\ntwo\nthree"),
                      "one\n...OUTPUT REMOVED (MAX LINES: 2)...\nthree")
Beispiel #11
0
 def testFill(self):
     obj = BasicFormatter(width=40)
     dots = obj.fill("andrew", "edward")
     self.assertEqual(dots, '.' * 12)
Beispiel #12
0
def fuzzer(seed=1980,
           timeout=(3, 10),
           max_fails=(15, 100),
           progress_interval=(3, 15),
           group_num=(15, 50),
           group_name_len=(6, 25),
           controller_num=(1, 6),
           controller_skip=0.05,
           controller_raise=0.05,
           controller_error=0.1,
           differ_num=(0, 3),
           differ_raise=0.01,
           differ_error=0.1,
           differ_fatal=0.1,
           differ_platform=0.1,
           differ_name_len=(6, 15),
           runner_num=(1, 3),
           runner_raise=0.01,
           runner_error=0.1,
           runner_fatal=0.05,
           runner_sleep=(0.5, 10),
           runner_platform=0.1,
           runner_name_len=(4, 29)):
    """
    A tool for calling `run` function with randomized test cases.
    """
    # This is more of a test object, so I wanted to keep the testing related import out of the
    # main functions for the run command.
    import random
    import string
    from moosetools.moosetest.formatters import BasicFormatter
    from moosetools.moosetest.base import make_runner, make_differ
    sys.path.append(os.path.join(os.path.dirname(__file__), 'tests'))
    from _helpers import TestController, TestRunner, TestDiffer

    def gen_name(rng):
        return ''.join(random.sample(string.ascii_letters, random.randint(*rng)))

    def gen_platform(ctrls, prob, kwargs):
        if random.uniform(0, 1) < prob:
            prefix = "{}_platform".format(random.choice(ctrls).getParam('prefix'))
            value = tuple(
                set(random.choices(['Darwin', 'Linux', 'Windows'], k=random.randint(1, 3))))
            kwargs[prefix] = value

    def gen_bool_with_odds(prob):
        return random.uniform(0, 1) < prob

    # Controller objects
    controllers = list()
    for i, n_controllers in enumerate(range(random.randint(*controller_num))):
        name_start = random.choice(string.ascii_letters)
        kwargs = dict()
        kwargs['stdout'] = True
        kwargs['stderr'] = True
        kwargs['prefix'] = "ctrl{:0.0f}".format(i)
        kwargs['skip'] = gen_bool_with_odds(controller_skip)
        kwargs['error'] = gen_bool_with_odds(controller_error)
        kwargs['raise'] = gen_bool_with_odds(controller_raise)
        controllers.append(TestController(object_name=name_start, **kwargs))
    controllers = tuple(controllers)

    # Runners/Differs
    groups = list()
    for n_groups in range(random.randint(*group_num)):
        runners = list()
        group_name = gen_name(group_name_len)
        for n_runners in range(random.randint(*runner_num)):
            differs = list()
            for n_differs in range(random.randint(*differ_num)):
                kwargs = dict()
                kwargs['name'] = gen_name(differ_name_len)
                kwargs['stdout'] = True
                kwargs['stderr'] = True
                kwargs['error'] = gen_bool_with_odds(differ_error)
                kwargs['raise'] = gen_bool_with_odds(differ_raise)
                kwargs['fatal'] = gen_bool_with_odds(differ_fatal)
                gen_platform(controllers, differ_platform, kwargs)
                differs.append(make_differ(TestDiffer, controllers, **kwargs))

            kwargs = dict()
            kwargs['name'] = f"{group_name}/{gen_name(runner_name_len)}"
            kwargs['differs'] = tuple(differs)
            kwargs['stdout'] = True
            kwargs['stderr'] = True
            kwargs['error'] = gen_bool_with_odds(runner_error)
            kwargs['raise'] = gen_bool_with_odds(runner_raise)
            kwargs['fatal'] = gen_bool_with_odds(runner_fatal)
            kwargs['sleep'] = random.uniform(*runner_sleep)
            gen_platform(controllers, runner_platform, kwargs)
            runners.append(make_runner(TestRunner, controllers, **kwargs))

        groups.append(runners)

    # Formatter
    kwargs = dict()
    kwargs['progress_interval'] = random.randint(*progress_interval)
    formatter = BasicFormatter(**kwargs)

    # Run
    kwargs = dict()
    kwargs['timeout'] = random.randint(*timeout)
    kwargs['max_fails'] = random.randint(*max_fails)
    kwargs['min_fail_state'] = random.choice([r for r in TestCase.Result])
    return run(groups, controllers, formatter, **kwargs)
Beispiel #13
0
        groups.append(runners)

    # Formatter
    kwargs = dict()
    kwargs['progress_interval'] = random.randint(*progress_interval)
    formatter = BasicFormatter(**kwargs)

    # Run
    kwargs = dict()
    kwargs['timeout'] = random.randint(*timeout)
    kwargs['max_fails'] = random.randint(*max_fails)
    kwargs['min_fail_state'] = random.choice([r for r in TestCase.Result])
    return run(groups, controllers, formatter, **kwargs)


if __name__ == '__main__':  # pragma: no cover
    # This is here for quick testing
    from moosetools.moosetest.formatters import BasicFormatter
    from moosetools.moosetest.base import make_runner, make_differ
    sys.path.append(os.path.join(os.path.dirname(__file__), 'tests'))
    from _helpers import TestController, TestRunner, TestDiffer

    fm = BasicFormatter(progress_interval=3)
    groups = [None] * 3
    groups[0] = [TestRunner(name='a.a', sleep=1), TestRunner(name='a.b', sleep=4)]
    groups[1] = [TestRunner(name='b.a', sleep=1), TestRunner(name='b.b', sleep=5)]
    groups[2] = [TestRunner(name='c.a', sleep=1), TestRunner(name='c.b', sleep=6)]

    Run(groups, tuple(), fm, n_threads=2)