summaryrefslogtreecommitdiff
path: root/unit_tests/test_capture_plugin.py
blob: edab7de0085221fc47c635cdafa73a698abe5ada (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
99
100
101
102
103
104
105
106
107
# -*- coding: utf-8 -*-
import sys
import unittest
from optparse import OptionParser
from nose.config import Config
from nose.plugins.capture import Capture

class TestCapturePlugin(unittest.TestCase):

    def setUp(self):
        self._stdout = sys.stdout

    def tearDown(self):
        sys.stdout = self._stdout

    def test_enabled_by_default(self):
        c = Capture()
        assert c.enabled

    def test_can_be_disabled(self):
        c = Capture()
        parser = OptionParser()
        c.addOptions(parser)
        options, args = parser.parse_args(['test_can_be_disabled',
                                           '-s'])
        c.configure(options, Config())
        assert not c.enabled

        c = Capture()
        options, args = parser.parse_args(['test_can_be_disabled_long',
                                           '--nocapture'])
        c.configure(options, Config())
        assert not c.enabled

        env = {'NOSE_NOCAPTURE': 1}
        c = Capture()
        parser = OptionParser()
        c.addOptions(parser, env)
        options, args = parser.parse_args(['test_can_be_disabled'])
        c.configure(options, Config())
        assert not c.enabled

        c = Capture()
        parser = OptionParser()
        c.addOptions(parser)
        
        options, args = parser.parse_args(['test_can_be_disabled'])
        c.configure(options, Config())
        assert c.enabled
        
    def test_captures_stdout(self):
        c = Capture()
        c.start()
        print "Hello"
        c.end()
        self.assertEqual(c.buffer, "Hello\n")
        
    def test_captures_nonascii_stdout(self):
        c = Capture()
        c.start()
        print "test 日本"
        c.end()
        self.assertEqual(c.buffer, "test 日本\n")

    def test_format_error(self):
        class Dummy:
            pass
        d = Dummy()
        c = Capture()
        c.start()
        try:
            print "Oh my!"
            raise Exception("boom")
        except:
            err = sys.exc_info()
        formatted = c.formatError(d, err)
        ec, ev, tb = err
        (fec, fev, ftb) = formatted
        # print fec, fev, ftb
        
        self.assertEqual(ec, fec)
        self.assertEqual(tb, ftb)
        assert 'Oh my!' in fev, "Output not found in error message"
        assert 'Oh my!' in d.capturedOutput, "Output not attached to test"

    def test_format_nonascii_error(self):
        class Dummy:
            pass
        d = Dummy()
        c = Capture()
        c.start()
        try:
            print "debug 日本"
            raise AssertionError(u'response does not contain 名')
        except:
            err = sys.exc_info()
        formatted = c.formatError(d, err)

    def test_captured_stdout_has_encoding_attribute(self):
        c = Capture()
        c.start()
        self.assertNotEqual(sys.stdout, sys.__stdout__)
        self.assertTrue(hasattr(sys.stdout, 'encoding'))
        c.end()

if __name__ == '__main__':
    unittest.main()