~ubuntu-branches/ubuntu/quantal/maas/quantal-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Copyright 2012 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Tests for `maastesting.scenarios`."""

from __future__ import (
    absolute_import,
    print_function,
    unicode_literals,
    )

__metaclass__ = type
__all__ = []

import unittest

from maastesting.scenarios import WithScenarios
from maastesting.testcase import TestCase


class TestWithScenarios(TestCase):

    def test_scenarios_applied(self):
        # Scenarios are applied correctly when a test is called via __call__()
        # instead of run().

        events = []

        class Test(WithScenarios, unittest.TestCase):

            scenarios = [
                ("one", dict(token="one")),
                ("two", dict(token="two")),
                ]

            def test(self):
                events.append(self.token)

        test = Test("test")
        test.__call__()

        self.assertEqual(["one", "two"], events)

    def test_scenarios_applied_by_call(self):
        # Scenarios are applied by __call__() when it is called first, and not
        # by run().

        events = []

        class Test(WithScenarios, unittest.TestCase):

            scenarios = [
                ("one", dict(token="one")),
                ("two", dict(token="two")),
                ]

            def test(self):
                events.append(self.token)

            def run(self, result=None):
                # Call-up right past WithScenarios.run() to show that it is
                # not responsible for applying scenarios, and __call__() is.
                super(WithScenarios, self).run(result)

        test = Test("test")
        test.__call__()

        self.assertEqual(["one", "two"], events)