PageRenderTime 48ms CodeModel.GetById 36ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/unit_tests/test_skip_plugin.py

https://bitbucket.org/jpellerin/nose/
Python | 130 lines | 104 code | 24 blank | 2 comment | 8 complexity | dbef33330d540aa31e1c89a31c38b2e3 MD5 | raw file
  1import unittest
  2from nose.config import Config
  3from nose.plugins.skip import Skip, SkipTest
  4from nose.result import TextTestResult
  5from StringIO import StringIO
  6from nose.result import _TextTestResult
  7from optparse import OptionParser
  8try:
  9    # 2.7+
 10    from unittest.runner import _WritelnDecorator
 11except ImportError:
 12    from unittest import _WritelnDecorator
 13
 14
 15class TestSkipPlugin(unittest.TestCase):
 16
 17    def test_api_present(self):
 18        sk = Skip()
 19        sk.addOptions
 20        sk.configure
 21        sk.prepareTestResult
 22
 23    def test_prepare_patches_result(self):
 24        stream = _WritelnDecorator(StringIO())
 25        res = _TextTestResult(stream, 0, 1)
 26        sk = Skip()
 27        sk.prepareTestResult(res)
 28        res._orig_addError
 29        res._orig_printErrors
 30        res._orig_wasSuccessful
 31        res.skipped
 32        self.assertEqual(res.errorClasses,
 33                         {SkipTest: (res.skipped, 'SKIP', False)})
 34
 35        # result w/out print works too
 36        res = unittest.TestResult()
 37        sk = Skip()
 38        sk.prepareTestResult(res)
 39        res._orig_addError
 40        res.skipped
 41        self.assertEqual(res.errorClasses,
 42                         {SkipTest: (res.skipped, 'SKIP', False)})
 43
 44    def test_patched_result_handles_skip(self):
 45        res = unittest.TestResult()
 46        sk = Skip()
 47        sk.prepareTestResult(res)
 48
 49        class TC(unittest.TestCase):
 50            def test(self):
 51                raise SkipTest('skip me')
 52
 53        test = TC('test')
 54        test(res)
 55        assert not res.errors, "Skip was not caught: %s" % res.errors
 56        assert res.skipped
 57        assert res.skipped[0][0] is test
 58
 59    def test_patches_only_when_needed(self):
 60        class NoPatch(unittest.TestResult):
 61            def __init__(self):
 62                self.errorClasses = {}
 63
 64        res = NoPatch()
 65        sk = Skip()
 66        sk.prepareTestResult(res)
 67        assert not hasattr(res, '_orig_addError'), \
 68               "Skip patched a result class it didn't need to patch"
 69
 70
 71    def test_skip_output(self):
 72        class TC(unittest.TestCase):
 73            def test(self):
 74                raise SkipTest('skip me')
 75
 76        stream = _WritelnDecorator(StringIO())
 77        res = _TextTestResult(stream, 0, 1)
 78        sk = Skip()
 79        sk.prepareTestResult(res)
 80
 81        test = TC('test')
 82        test(res)
 83        assert not res.errors, "Skip was not caught: %s" % res.errors
 84        assert res.skipped
 85
 86        res.printErrors()
 87        out = stream.getvalue()
 88        print out
 89        assert out
 90        assert out.strip() == "S"
 91        assert res.wasSuccessful()
 92
 93    def test_skip_output_verbose(self):
 94
 95        class TC(unittest.TestCase):
 96            def test(self):
 97                raise SkipTest('skip me too')
 98
 99        stream = _WritelnDecorator(StringIO())
100        res = _TextTestResult(stream, 0, verbosity=2)
101        sk = Skip()
102        sk.prepareTestResult(res)
103        test = TC('test')
104        test(res)
105        assert not res.errors, "Skip was not caught: %s" % res.errors
106        assert res.skipped
107
108        res.printErrors()
109        out = stream.getvalue()
110        print out
111        assert out
112
113        assert ' ... SKIP' in out
114        assert 'skip me too' in out
115
116    def test_enabled_by_default(self):
117        sk = Skip()
118        assert sk.enabled, "Skip was not enabled by default"
119
120    def test_can_be_disabled(self):
121        parser = OptionParser()
122        sk = Skip()
123        sk.addOptions(parser)
124        options, args = parser.parse_args(['--no-skip'])
125        sk.configure(options, Config())
126        assert not sk.enabled, "Skip was not disabled by noSkip option"
127        
128
129if __name__ == '__main__':
130    unittest.main()