summaryrefslogtreecommitdiff
path: root/lib/testtools/testtools/tests/test_distutilscmd.py
blob: c485a473d3939169c115ccf08a19fb2aa682aabd (plain)
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
# Copyright (c) 2010-2011 Testtools authors. See LICENSE for details.

"""Tests for the distutils test command logic."""

from distutils.dist import Distribution

from testtools.compat import (
    _b,
    BytesIO,
    )
from testtools.helpers import try_import
fixtures = try_import('fixtures')

import testtools
from testtools import TestCase
from testtools.distutilscmd import TestCommand
from testtools.matchers import MatchesRegex


if fixtures:
    class SampleTestFixture(fixtures.Fixture):
        """Creates testtools.runexample temporarily."""

        def __init__(self):
            self.package = fixtures.PythonPackage(
            'runexample', [('__init__.py', _b("""
from testtools import TestCase

class TestFoo(TestCase):
    def test_bar(self):
        pass
    def test_quux(self):
        pass
def test_suite():
    from unittest import TestLoader
    return TestLoader().loadTestsFromName(__name__)
"""))])

        def setUp(self):
            super(SampleTestFixture, self).setUp()
            self.useFixture(self.package)
            testtools.__path__.append(self.package.base)
            self.addCleanup(testtools.__path__.remove, self.package.base)


class TestCommandTest(TestCase):

    def setUp(self):
        super(TestCommandTest, self).setUp()
        if fixtures is None:
            self.skipTest("Need fixtures")

    def test_test_module(self):
        self.useFixture(SampleTestFixture())
        stream = BytesIO()
        dist = Distribution()
        dist.script_name = 'setup.py'
        dist.script_args = ['test']
        dist.cmdclass = {'test': TestCommand}
        dist.command_options = {
            'test': {'test_module': ('command line', 'testtools.runexample')}}
        cmd = dist.reinitialize_command('test')
        cmd.runner.stdout = stream
        dist.run_command('test')
        self.assertThat(
            stream.getvalue(),
            MatchesRegex(_b("""Tests running...

Ran 2 tests in \\d.\\d\\d\\ds
OK
""")))

    def test_test_suite(self):
        self.useFixture(SampleTestFixture())
        stream = BytesIO()
        dist = Distribution()
        dist.script_name = 'setup.py'
        dist.script_args = ['test']
        dist.cmdclass = {'test': TestCommand}
        dist.command_options = {
            'test': {
                'test_suite': (
                    'command line', 'testtools.runexample.test_suite')}}
        cmd = dist.reinitialize_command('test')
        cmd.runner.stdout = stream
        dist.run_command('test')
        self.assertThat(
            stream.getvalue(),
            MatchesRegex(_b("""Tests running...

Ran 2 tests in \\d.\\d\\d\\ds
OK
""")))


def test_suite():
    from unittest import TestLoader
    return TestLoader().loadTestsFromName(__name__)