File: test_testcase.py

package info (click to toggle)
python-fixtures 4.2.5-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 408 kB
  • sloc: python: 2,547; makefile: 29; sh: 9
file content (115 lines) | stat: -rw-r--r-- 4,196 bytes parent folder | download
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#  fixtures: Fixtures with cleanups for testing and convenience.
#
# Copyright (c) 2010, Robert Collins <robertc@robertcollins.net>
#
# Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
# license at the users choice. A copy of both licenses are available in the
# project source as Apache-2.0 and BSD. You may not use this file except in
# compliance with one of these two licences.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# license you chose for the specific language governing permissions and
# limitations under that license.

import unittest
import testtools
from testtools.content import text_content
from testtools.testcase import skipIf

import fixtures
from fixtures import TestWithFixtures
from fixtures.fixture import gather_details
from fixtures.tests.helpers import LoggingFixture


class TestTestWithFixtures(unittest.TestCase):
    def test_useFixture(self):
        fixture = LoggingFixture()

        class SimpleTest(testtools.TestCase, TestWithFixtures):
            def test_foo(self):
                self.useFixture(fixture)

        result = unittest.TestResult()
        SimpleTest('test_foo').run(result)
        self.assertTrue(result.wasSuccessful())
        self.assertEqual(['setUp', 'cleanUp'], fixture.calls)

    def test_useFixture_uses_raise_first(self):
        calls = []

        def raiser(ignored):
            calls.append('called')
            raise Exception('foo')

        fixture = fixtures.FunctionFixture(lambda: None, raiser)

        class SimpleTest(testtools.TestCase, TestWithFixtures):
            def test_foo(self):
                self.useFixture(fixture)

        result = unittest.TestResult()
        SimpleTest('test_foo').run(result)
        self.assertFalse(result.wasSuccessful())
        self.assertEqual(['called'], calls)

    @skipIf(gather_details is None, "gather_details() is not available.")
    def test_useFixture_details_captured_from_setUp(self):
        # Details added during fixture set-up are gathered even if setUp()
        # fails with an exception.
        class SomethingBroke(Exception):
            pass

        class BrokenFixture(fixtures.Fixture):
            def setUp(self):
                super(BrokenFixture, self).setUp()
                self.addDetail('content', text_content("foobar"))
                raise SomethingBroke()

        broken_fixture = BrokenFixture()

        class DetailedTestCase(TestWithFixtures, testtools.TestCase):
            def setUp(self):
                super(DetailedTestCase, self).setUp()
                self.useFixture(broken_fixture)

            def test(self):
                pass

        detailed_test_case = DetailedTestCase("test")
        self.assertRaises(SomethingBroke, detailed_test_case.setUp)
        self.assertEqual(
            {"content": text_content("foobar")}, broken_fixture.getDetails()
        )
        self.assertEqual(
            {"content": text_content("foobar")},
            detailed_test_case.getDetails(),
        )

    @skipIf(gather_details is None, "gather_details() is not available.")
    def test_useFixture_details_not_captured_from_setUp(self):
        # Details added during fixture set-up are not gathered if the test
        # case does not have the ability to accept those details.
        class SomethingBroke(Exception):
            pass

        class BrokenFixture(fixtures.Fixture):
            def setUp(self):
                super(BrokenFixture, self).setUp()
                self.addDetail('content', text_content("foobar"))
                raise SomethingBroke()

        broken_fixture = BrokenFixture()

        class NonDetailedTestCase(TestWithFixtures, unittest.TestCase):
            def setUp(self):
                super(NonDetailedTestCase, self).setUp()
                self.useFixture(broken_fixture)

            def test(self):
                pass

        non_detailed_test_case = NonDetailedTestCase("test")
        self.assertRaises(SomethingBroke, non_detailed_test_case.setUp)