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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
import os
import re
import sys
import tempfile
import unittest
from nose.plugins import PluginTester
from nose.plugins.builtin import Doctest
from nose.plugins.builtin import TestId
from cPickle import dump, load
support = os.path.join(os.path.dirname(__file__), 'support')
idfile = tempfile.mktemp()
test_part = re.compile(r'(#\d+)? +([^(]+)')
def teardown():
try:
os.remove(idfile)
except OSError:
pass
class TestDiscoveryMode(PluginTester, unittest.TestCase):
activate = '--with-id'
plugins = [TestId()]
args = ['-v', '--id-file=%s' % idfile]
suitepath = os.path.join(support, 'idp')
def test_ids_added_to_output(self):
#print '>' * 70
#print str(self.output)
#print '<' * 70
for line in self.output:
if line.startswith('='):
break
if not line.strip():
continue
if 'test_gen' in line and not '(0,)' in line:
assert not line.startswith('#'), \
"Generated test line '%s' should not have id" % line
else:
assert line.startswith('#'), \
"Test line '%s' missing id" % line.strip()
# test that id file is written
def test_id_file_contains_ids_seen(self):
assert os.path.exists(idfile)
fh = open(idfile, 'rb')
ids = load(fh)['ids']
fh.close()
assert ids
assert ids.keys()
self.assertEqual(map(int, ids.keys()), ids.keys())
assert ids.values()
class TestLoadNamesMode(PluginTester, unittest.TestCase):
"""NOTE that this test passing requires the previous test case to
be run! (Otherwise the ids file will not exist)
"""
activate = '--with-id'
plugins = [TestId()]
# Not a typo: # is optional before ids
args = ['-v', '--id-file=%s' % idfile, '2', '#5']
suitepath = None
def makeSuite(self):
return None
def test_load_ids(self):
#print '#' * 70
#print str(self.output)
#print '#' * 70
for line in self.output:
if line.startswith('#'):
assert line.startswith('#2 ') or line.startswith('#5 '), \
"Unexpected test line '%s'" % line
assert os.path.exists(idfile)
fh = open(idfile, 'rb')
ids = load(fh)
fh.close()
assert ids
assert ids.keys()
ids = ids['ids']
self.assertEqual(filter(lambda i: int(i), ids.keys()), ids.keys())
assert len(ids.keys()) > 2
class TestLoadNamesMode_2(PluginTester, unittest.TestCase):
"""NOTE that this test passing requires the previous test case to
be run! (Otherwise the ids file will not exist)
Tests that generators still only have id on one line
"""
activate = '--with-id'
plugins = [TestId()]
args = ['-v', '--id-file=%s' % idfile, '9']
suitepath = None
def makeSuite(self):
return None
def test_load_ids(self):
#print '%' * 70
#print str(self.output)
#print '%' * 70
count = 0
for line in self.output:
if line.startswith('#'):
count += 1
self.assertEqual(count, 1)
teardown()
class TestWithDoctest_1(PluginTester, unittest.TestCase):
activate = '--with-id'
plugins = [Doctest(), TestId()]
args = ['-v', '--id-file=%s' % idfile, '--with-doctest']
suitepath = os.path.join(support, 'idp')
def test_doctests_get_ids(self):
#print '>' * 70
#print str(self.output)
#print '>' * 70
last = None
for line in self.output:
if line.startswith('='):
break
if not line.strip():
continue
# assert line startswith # or test part matches last
m = test_part.match(line.rstrip())
assert m
idx, name = m.groups()
assert idx or last is None or name == last, \
"Expected an id on line %s" % line.strip()
last = name
fh = open(idfile, 'rb')
ids = load(fh)['ids']
fh.close()
for key, (file, mod, call) in ids.items():
assert mod != 'doctest', \
"Doctest test was incorrectly identified as being part of "\
"the doctest module itself (#%s)" % key
class TestWithDoctest_2(PluginTester, unittest.TestCase):
activate = '--with-id'
plugins = [Doctest(), TestId()]
args = ['-v', '--id-file=%s' % idfile, '--with-doctest', '#2']
suitepath = None
def setUp(self):
sys.path.insert(0, os.path.join(support, 'idp'))
super(TestWithDoctest_2, self).setUp()
def tearDown(self):
sys.path.remove(os.path.join(support, 'idp'))
super(TestWithDoctest_2, self).tearDown()
def makeSuite(self):
return None
def test_load_ids_doctest(self):
print '*' * 70
print str(self.output)
print '*' * 70
assert 'Doctest: exm.add_one ... FAIL' in self.output
count = 0
for line in self.output:
if line.startswith('#'):
count += 1
self.assertEqual(count, 1)
teardown()
class TestWithDoctestFileTests_1(PluginTester, unittest.TestCase):
activate = '--with-id'
plugins = [Doctest(), TestId()]
args = ['-v', '--id-file=%s' % idfile, '--with-doctest',
'--doctest-extension=.txt']
suitepath = os.path.join(support, 'dtt', 'docs')
def test_docfile_tests_get_ids(self):
print '>' * 70
print str(self.output)
print '>' * 70
last = None
for line in self.output:
if line.startswith('='):
break
# assert line startswith # or test part matches last
if not line.strip():
continue
m = test_part.match(line.rstrip())
assert m, "line %s does not match expected pattern" % line.strip()
idx, name = m.groups()
assert idx or last is None or name == last, \
"Expected an id on line %s" % line.strip()
last = name
fh = open(idfile, 'rb')
ids = load(fh)['ids']
fh.close()
for key, (file, mod, call) in ids.items():
assert mod != 'doctest', \
"Doctest test was incorrectly identified as being part of "\
"the doctest module itself (#%s)" % key
class TestWithDoctestFileTests_2(PluginTester, unittest.TestCase):
activate = '--with-id'
plugins = [Doctest(), TestId()]
args = ['-v', '--id-file=%s' % idfile, '--with-doctest',
'--doctest-extension=.txt', '2']
suitepath = None
def setUp(self):
sys.path.insert(0, os.path.join(support, 'dtt', 'docs'))
super(TestWithDoctestFileTests_2, self).setUp()
def tearDown(self):
sys.path.remove(os.path.join(support, 'dtt', 'docs'))
super(TestWithDoctestFileTests_2, self).tearDown()
def makeSuite(self):
return None
def test_load_from_name_id_docfile_test(self):
print '*' * 70
print str(self.output)
print '*' * 70
assert 'Doctest: errdoc.txt ... FAIL' in self.output
count = 0
for line in self.output:
if line.startswith('#'):
count += 1
assert count == 1
teardown()
if __name__ == '__main__':
import logging
logging.basicConfig()
l = logging.getLogger('nose.plugins.testid')
l.setLevel(logging.DEBUG)
try:
unittest.main()
finally:
teardown()
|