| 1 |
# |
| 2 |
# subunit: extensions to Python unittest to get test results from subprocesses. |
| 3 |
# Copyright (C) 2005 Robert Collins <[email protected]> |
| 4 |
# |
| 5 |
# Licensed under either the Apache License, Version 2.0 or the BSD 3-clause |
| 6 |
# license at the users choice. A copy of both licenses are available in the |
| 7 |
# project source as Apache-2.0 and BSD. You may not use this file except in |
| 8 |
# compliance with one of these two licences. |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under these licenses is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# license you chose for the specific language governing permissions and |
| 14 |
# limitations under that license. |
| 15 |
# |
| 16 |
|
| 17 |
import datetime |
| 18 |
import io |
| 19 |
import os |
| 20 |
import tempfile |
| 21 |
import unittest |
| 22 |
from io import BytesIO |
| 23 |
|
| 24 |
from testtools import PlaceHolder, TestCase, TestResult, skipIf |
| 25 |
from testtools.content import Content, TracebackContent, text_content |
| 26 |
from testtools.content_type import ContentType |
| 27 |
|
| 28 |
from testtools.testresult.doubles import ExtendedTestResult |
| 29 |
|
| 30 |
from testtools.matchers import Contains, Equals, MatchesAny |
| 31 |
|
| 32 |
import iso8601 |
| 33 |
|
| 34 |
import subunit |
| 35 |
from . import ( |
| 36 |
_remote_exception_repr, |
| 37 |
_remote_exception_repr_chunked, |
| 38 |
_remote_exception_str, |
| 39 |
_remote_exception_str_chunked, |
| 40 |
) |
| 41 |
|
| 42 |
tb_prelude = "Traceback (most recent call last):\n" |
| 43 |
|
| 44 |
|
| 45 |
def details_to_str(details): |
| 46 |
return TestResult()._err_details_to_string(None, details=details) |
| 47 |
|
| 48 |
|
| 49 |
class TestHelpers(TestCase): |
| 50 |
def test__unwrap_text_file_read_mode(self): |
| 51 |
fd, file_path = tempfile.mkstemp() |
| 52 |
self.addCleanup(os.remove, file_path) |
| 53 |
fake_file = os.fdopen(fd, "r") |
| 54 |
self.assertEqual(fake_file.buffer, subunit._unwrap_text(fake_file)) |
| 55 |
|
| 56 |
def test__unwrap_text_file_write_mode(self): |
| 57 |
fd, file_path = tempfile.mkstemp() |
| 58 |
self.addCleanup(os.remove, file_path) |
| 59 |
fake_file = os.fdopen(fd, "w") |
| 60 |
self.assertEqual(fake_file.buffer, subunit._unwrap_text(fake_file)) |
| 61 |
|
| 62 |
def test__unwrap_text_fileIO_read_mode(self): |
| 63 |
fd, file_path = tempfile.mkstemp() |
| 64 |
self.addCleanup(os.remove, file_path) |
| 65 |
fake_file = io.FileIO(file_path, "r") |
| 66 |
self.assertEqual(fake_file, subunit._unwrap_text(fake_file)) |
| 67 |
|
| 68 |
def test__unwrap_text_fileIO_write_mode(self): |
| 69 |
fd, file_path = tempfile.mkstemp() |
| 70 |
self.addCleanup(os.remove, file_path) |
| 71 |
fake_file = io.FileIO(file_path, "w") |
| 72 |
self.assertEqual(fake_file, subunit._unwrap_text(fake_file)) |
| 73 |
|
| 74 |
def test__unwrap_text_BytesIO(self): |
| 75 |
fake_stream = io.BytesIO() |
| 76 |
self.assertEqual(fake_stream, subunit._unwrap_text(fake_stream)) |
| 77 |
|
| 78 |
|
| 79 |
class TestTestImports(unittest.TestCase): |
| 80 |
def test_imports(self): |
| 81 |
from subunit import ( # noqa: F401 |
| 82 |
DiscardStream, |
| 83 |
ExecTestCase, |
| 84 |
IsolatedTestCase, |
| 85 |
ProtocolTestCase, |
| 86 |
RemotedTestCase, |
| 87 |
RemoteError, |
| 88 |
TestProtocolClient, |
| 89 |
TestProtocolServer, |
| 90 |
) # noqa: F401 |
| 91 |
|
| 92 |
|
| 93 |
class TestDiscardStream(unittest.TestCase): |
| 94 |
def test_write(self): |
| 95 |
subunit.DiscardStream().write("content") |
| 96 |
|
| 97 |
|
| 98 |
class TestProtocolServerForward(unittest.TestCase): |
| 99 |
def test_story(self): |
| 100 |
client = unittest.TestResult() |
| 101 |
out = BytesIO() |
| 102 |
protocol = subunit.TestProtocolServer(client, forward_stream=out) |
| 103 |
pipe = BytesIO(b"test old mcdonald\nsuccess old mcdonald\n") |
| 104 |
protocol.readFrom(pipe) |
| 105 |
self.assertEqual(client.testsRun, 1) |
| 106 |
self.assertEqual(pipe.getvalue(), out.getvalue()) |
| 107 |
|
| 108 |
def test_not_command(self): |
| 109 |
client = unittest.TestResult() |
| 110 |
out = BytesIO() |
| 111 |
protocol = subunit.TestProtocolServer(client, stream=subunit.DiscardStream(), forward_stream=out) |
| 112 |
pipe = BytesIO(b"success old mcdonald\n") |
| 113 |
protocol.readFrom(pipe) |
| 114 |
self.assertEqual(client.testsRun, 0) |
| 115 |
self.assertEqual(b"", out.getvalue()) |
| 116 |
|
| 117 |
|
| 118 |
class TestTestProtocolServerPipe(unittest.TestCase): |
| 119 |
def test_story(self): |
| 120 |
client = unittest.TestResult() |
| 121 |
protocol = subunit.TestProtocolServer(client) |
| 122 |
traceback = "foo.c:53:ERROR invalid state\n" |
| 123 |
pipe = BytesIO( |
| 124 |
b"test old mcdonald\n" |
| 125 |
b"success old mcdonald\n" |
| 126 |
b"test bing crosby\n" |
| 127 |
b"failure bing crosby [\n" + traceback.encode() + b"]\n" |
| 128 |
b"test an error\n" |
| 129 |
b"error an error\n" |
| 130 |
) |
| 131 |
protocol.readFrom(pipe) |
| 132 |
bing = subunit.RemotedTestCase("bing crosby") |
| 133 |
an_error = subunit.RemotedTestCase("an error") |
| 134 |
self.assertEqual( |
| 135 |
client.errors, |
| 136 |
[(an_error, _remote_exception_repr + "\n")], |
| 137 |
) |
| 138 |
self.assertEqual( |
| 139 |
client.failures, |
| 140 |
[(bing, _remote_exception_repr + ": " + details_to_str({"traceback": text_content(traceback)}) + "\n")], |
| 141 |
) |
| 142 |
self.assertEqual(client.testsRun, 3) |
| 143 |
|
| 144 |
def test_non_test_characters_forwarded_immediately(self): |
| 145 |
pass |
| 146 |
|
| 147 |
|
| 148 |
class TestTestProtocolServerStartTest(unittest.TestCase): |
| 149 |
def setUp(self): |
| 150 |
self.client = ExtendedTestResult() |
| 151 |
self.stream = BytesIO() |
| 152 |
self.protocol = subunit.TestProtocolServer(self.client, self.stream) |
| 153 |
|
| 154 |
def test_start_test(self): |
| 155 |
self.protocol.lineReceived(b"test old mcdonald\n") |
| 156 |
self.assertEqual(self.client._events, [("startTest", subunit.RemotedTestCase("old mcdonald"))]) |
| 157 |
|
| 158 |
def test_start_testing(self): |
| 159 |
self.protocol.lineReceived(b"testing old mcdonald\n") |
| 160 |
self.assertEqual(self.client._events, [("startTest", subunit.RemotedTestCase("old mcdonald"))]) |
| 161 |
|
| 162 |
def test_start_test_colon(self): |
| 163 |
self.protocol.lineReceived(b"test: old mcdonald\n") |
| 164 |
self.assertEqual(self.client._events, [("startTest", subunit.RemotedTestCase("old mcdonald"))]) |
| 165 |
|
| 166 |
def test_indented_test_colon_ignored(self): |
| 167 |
ignored_line = b" test: old mcdonald\n" |
| 168 |
self.protocol.lineReceived(ignored_line) |
| 169 |
self.assertEqual([], self.client._events) |
| 170 |
self.assertEqual(self.stream.getvalue(), ignored_line) |
| 171 |
|
| 172 |
def test_start_testing_colon(self): |
| 173 |
self.protocol.lineReceived(b"testing: old mcdonald\n") |
| 174 |
self.assertEqual(self.client._events, [("startTest", subunit.RemotedTestCase("old mcdonald"))]) |
| 175 |
|
| 176 |
|
| 177 |
class TestTestProtocolServerPassThrough(unittest.TestCase): |
| 178 |
def setUp(self): |
| 179 |
self.stdout = BytesIO() |
| 180 |
self.test = subunit.RemotedTestCase("old mcdonald") |
| 181 |
self.client = ExtendedTestResult() |
| 182 |
self.protocol = subunit.TestProtocolServer(self.client, self.stdout) |
| 183 |
|
| 184 |
def keywords_before_test(self): |
| 185 |
self.protocol.lineReceived(b"failure a\n") |
| 186 |
self.protocol.lineReceived(b"failure: a\n") |
| 187 |
self.protocol.lineReceived(b"error a\n") |
| 188 |
self.protocol.lineReceived(b"error: a\n") |
| 189 |
self.protocol.lineReceived(b"success a\n") |
| 190 |
self.protocol.lineReceived(b"success: a\n") |
| 191 |
self.protocol.lineReceived(b"successful a\n") |
| 192 |
self.protocol.lineReceived(b"successful: a\n") |
| 193 |
self.protocol.lineReceived(b"]\n") |
| 194 |
self.assertEqual( |
| 195 |
self.stdout.getvalue(), |
| 196 |
b"failure a\nfailure: a\nerror a\nerror: a\nsuccess a\nsuccess: a\nsuccessful a\nsuccessful: a\n]\n", |
| 197 |
) |
| 198 |
|
| 199 |
def test_keywords_before_test(self): |
| 200 |
self.keywords_before_test() |
| 201 |
self.assertEqual(self.client._events, []) |
| 202 |
|
| 203 |
def test_keywords_after_error(self): |
| 204 |
self.protocol.lineReceived(b"test old mcdonald\n") |
| 205 |
self.protocol.lineReceived(b"error old mcdonald\n") |
| 206 |
self.keywords_before_test() |
| 207 |
self.assertEqual( |
| 208 |
[ |
| 209 |
("startTest", self.test), |
| 210 |
("addError", self.test, {}), |
| 211 |
("stopTest", self.test), |
| 212 |
], |
| 213 |
self.client._events, |
| 214 |
) |
| 215 |
|
| 216 |
def test_keywords_after_failure(self): |
| 217 |
self.protocol.lineReceived(b"test old mcdonald\n") |
| 218 |
self.protocol.lineReceived(b"failure old mcdonald\n") |
| 219 |
self.keywords_before_test() |
| 220 |
self.assertEqual( |
| 221 |
self.client._events, |
| 222 |
[ |
| 223 |
("startTest", self.test), |
| 224 |
("addFailure", self.test, {}), |
| 225 |
("stopTest", self.test), |
| 226 |
], |
| 227 |
) |
| 228 |
|
| 229 |
def test_keywords_after_success(self): |
| 230 |
self.protocol.lineReceived(b"test old mcdonald\n") |
| 231 |
self.protocol.lineReceived(b"success old mcdonald\n") |
| 232 |
self.keywords_before_test() |
| 233 |
self.assertEqual( |
| 234 |
[ |
| 235 |
("startTest", self.test), |
| 236 |
("addSuccess", self.test), |
| 237 |
("stopTest", self.test), |
| 238 |
], |
| 239 |
self.client._events, |
| 240 |
) |
| 241 |
|
| 242 |
def test_keywords_after_test(self): |
| 243 |
self.protocol.lineReceived(b"test old mcdonald\n") |
| 244 |
self.protocol.lineReceived(b"test old mcdonald\n") |
| 245 |
self.protocol.lineReceived(b"failure a\n") |
| 246 |
self.protocol.lineReceived(b"failure: a\n") |
| 247 |
self.protocol.lineReceived(b"error a\n") |
| 248 |
self.protocol.lineReceived(b"error: a\n") |
| 249 |
self.protocol.lineReceived(b"success a\n") |
| 250 |
self.protocol.lineReceived(b"success: a\n") |
| 251 |
self.protocol.lineReceived(b"successful a\n") |
| 252 |
self.protocol.lineReceived(b"successful: a\n") |
| 253 |
self.protocol.lineReceived(b"]\n") |
| 254 |
self.protocol.lineReceived(b"failure old mcdonald\n") |
| 255 |
self.assertEqual( |
| 256 |
self.stdout.getvalue(), |
| 257 |
b"test old mcdonald\n" |
| 258 |
b"failure a\n" |
| 259 |
b"failure: a\n" |
| 260 |
b"error a\n" |
| 261 |
b"error: a\n" |
| 262 |
b"success a\n" |
| 263 |
b"success: a\n" |
| 264 |
b"successful a\n" |
| 265 |
b"successful: a\n" |
| 266 |
b"]\n", |
| 267 |
) |
| 268 |
self.assertEqual( |
| 269 |
self.client._events, |
| 270 |
[ |
| 271 |
("startTest", self.test), |
| 272 |
("addFailure", self.test, {}), |
| 273 |
("stopTest", self.test), |
| 274 |
], |
| 275 |
) |
| 276 |
|
| 277 |
def test_keywords_during_failure(self): |
| 278 |
# A smoke test to make sure that the details parsers have control |
| 279 |
# appropriately. |
| 280 |
self.protocol.lineReceived(b"test old mcdonald\n") |
| 281 |
self.protocol.lineReceived(b"failure: old mcdonald [\n") |
| 282 |
self.protocol.lineReceived(b"test old mcdonald\n") |
| 283 |
self.protocol.lineReceived(b"failure a\n") |
| 284 |
self.protocol.lineReceived(b"failure: a\n") |
| 285 |
self.protocol.lineReceived(b"error a\n") |
| 286 |
self.protocol.lineReceived(b"error: a\n") |
| 287 |
self.protocol.lineReceived(b"success a\n") |
| 288 |
self.protocol.lineReceived(b"success: a\n") |
| 289 |
self.protocol.lineReceived(b"successful a\n") |
| 290 |
self.protocol.lineReceived(b"successful: a\n") |
| 291 |
self.protocol.lineReceived(b" ]\n") |
| 292 |
self.protocol.lineReceived(b"]\n") |
| 293 |
self.assertEqual(self.stdout.getvalue(), b"") |
| 294 |
details = {} |
| 295 |
details["traceback"] = Content( |
| 296 |
ContentType("text", "x-traceback", {"charset": "utf8"}), |
| 297 |
lambda: [ |
| 298 |
b"test old mcdonald\n" |
| 299 |
b"failure a\n" |
| 300 |
b"failure: a\n" |
| 301 |
b"error a\n" |
| 302 |
b"error: a\n" |
| 303 |
b"success a\n" |
| 304 |
b"success: a\n" |
| 305 |
b"successful a\n" |
| 306 |
b"successful: a\n" |
| 307 |
b"]\n" |
| 308 |
], |
| 309 |
) |
| 310 |
self.assertEqual( |
| 311 |
self.client._events, |
| 312 |
[ |
| 313 |
("startTest", self.test), |
| 314 |
("addFailure", self.test, details), |
| 315 |
("stopTest", self.test), |
| 316 |
], |
| 317 |
) |
| 318 |
|
| 319 |
def test_stdout_passthrough(self): |
| 320 |
"""Lines received which cannot be interpreted as any protocol action |
| 321 |
should be passed through to sys.stdout. |
| 322 |
""" |
| 323 |
bytes = b"randombytes\n" |
| 324 |
self.protocol.lineReceived(bytes) |
| 325 |
self.assertEqual(self.stdout.getvalue(), bytes) |
| 326 |
|
| 327 |
|
| 328 |
class TestTestProtocolServerLostConnection(unittest.TestCase): |
| 329 |
def setUp(self): |
| 330 |
self.client = ExtendedTestResult() |
| 331 |
self.protocol = subunit.TestProtocolServer(self.client) |
| 332 |
self.test = subunit.RemotedTestCase("old mcdonald") |
| 333 |
|
| 334 |
def test_lost_connection_no_input(self): |
| 335 |
self.protocol.lostConnection() |
| 336 |
self.assertEqual([], self.client._events) |
| 337 |
|
| 338 |
def test_lost_connection_after_start(self): |
| 339 |
self.protocol.lineReceived(b"test old mcdonald\n") |
| 340 |
self.protocol.lostConnection() |
| 341 |
failure = subunit.RemoteError("lost connection during test 'old mcdonald'") |
| 342 |
self.assertEqual( |
| 343 |
[ |
| 344 |
("startTest", self.test), |
| 345 |
("addError", self.test, failure), |
| 346 |
("stopTest", self.test), |
| 347 |
], |
| 348 |
self.client._events, |
| 349 |
) |
| 350 |
|
| 351 |
def test_lost_connected_after_error(self): |
| 352 |
self.protocol.lineReceived(b"test old mcdonald\n") |
| 353 |
self.protocol.lineReceived(b"error old mcdonald\n") |
| 354 |
self.protocol.lostConnection() |
| 355 |
self.assertEqual( |
| 356 |
[ |
| 357 |
("startTest", self.test), |
| 358 |
("addError", self.test, {}), |
| 359 |
("stopTest", self.test), |
| 360 |
], |
| 361 |
self.client._events, |
| 362 |
) |
| 363 |
|
| 364 |
def do_connection_lost(self, outcome, opening): |
| 365 |
self.protocol.lineReceived(b"test old mcdonald\n") |
| 366 |
self.protocol.lineReceived("{} old mcdonald {}".format(outcome, opening).encode()) |
| 367 |
self.protocol.lostConnection() |
| 368 |
failure = subunit.RemoteError("lost connection during %s report of test 'old mcdonald'" % outcome) |
| 369 |
self.assertEqual( |
| 370 |
[ |
| 371 |
("startTest", self.test), |
| 372 |
("addError", self.test, failure), |
| 373 |
("stopTest", self.test), |
| 374 |
], |
| 375 |
self.client._events, |
| 376 |
) |
| 377 |
|
| 378 |
def test_lost_connection_during_error(self): |
| 379 |
self.do_connection_lost("error", "[\n") |
| 380 |
|
| 381 |
def test_lost_connection_during_error_details(self): |
| 382 |
self.do_connection_lost("error", "[ multipart\n") |
| 383 |
|
| 384 |
def test_lost_connected_after_failure(self): |
| 385 |
self.protocol.lineReceived(b"test old mcdonald\n") |
| 386 |
self.protocol.lineReceived(b"failure old mcdonald\n") |
| 387 |
self.protocol.lostConnection() |
| 388 |
self.assertEqual( |
| 389 |
[ |
| 390 |
("startTest", self.test), |
| 391 |
("addFailure", self.test, {}), |
| 392 |
("stopTest", self.test), |
| 393 |
], |
| 394 |
self.client._events, |
| 395 |
) |
| 396 |
|
| 397 |
def test_lost_connection_during_failure(self): |
| 398 |
self.do_connection_lost("failure", "[\n") |
| 399 |
|
| 400 |
def test_lost_connection_during_failure_details(self): |
| 401 |
self.do_connection_lost("failure", "[ multipart\n") |
| 402 |
|
| 403 |
def test_lost_connection_after_success(self): |
| 404 |
self.protocol.lineReceived(b"test old mcdonald\n") |
| 405 |
self.protocol.lineReceived(b"success old mcdonald\n") |
| 406 |
self.protocol.lostConnection() |
| 407 |
self.assertEqual( |
| 408 |
[ |
| 409 |
("startTest", self.test), |
| 410 |
("addSuccess", self.test), |
| 411 |
("stopTest", self.test), |
| 412 |
], |
| 413 |
self.client._events, |
| 414 |
) |
| 415 |
|
| 416 |
def test_lost_connection_during_success(self): |
| 417 |
self.do_connection_lost("success", "[\n") |
| 418 |
|
| 419 |
def test_lost_connection_during_success_details(self): |
| 420 |
self.do_connection_lost("success", "[ multipart\n") |
| 421 |
|
| 422 |
def test_lost_connection_during_skip(self): |
| 423 |
self.do_connection_lost("skip", "[\n") |
| 424 |
|
| 425 |
def test_lost_connection_during_skip_details(self): |
| 426 |
self.do_connection_lost("skip", "[ multipart\n") |
| 427 |
|
| 428 |
def test_lost_connection_during_xfail(self): |
| 429 |
self.do_connection_lost("xfail", "[\n") |
| 430 |
|
| 431 |
def test_lost_connection_during_xfail_details(self): |
| 432 |
self.do_connection_lost("xfail", "[ multipart\n") |
| 433 |
|
| 434 |
def test_lost_connection_during_uxsuccess(self): |
| 435 |
self.do_connection_lost("uxsuccess", "[\n") |
| 436 |
|
| 437 |
def test_lost_connection_during_uxsuccess_details(self): |
| 438 |
self.do_connection_lost("uxsuccess", "[ multipart\n") |
| 439 |
|
| 440 |
|
| 441 |
class TestInTestMultipart(unittest.TestCase): |
| 442 |
def setUp(self): |
| 443 |
self.client = ExtendedTestResult() |
| 444 |
self.protocol = subunit.TestProtocolServer(self.client) |
| 445 |
self.protocol.lineReceived(b"test mcdonalds farm\n") |
| 446 |
self.test = subunit.RemotedTestCase("mcdonalds farm") |
| 447 |
|
| 448 |
def test__outcome_sets_details_parser(self): |
| 449 |
self.protocol._reading_success_details.details_parser = None |
| 450 |
self.protocol._state._outcome(0, b"mcdonalds farm [ multipart\n", None, self.protocol._reading_success_details) |
| 451 |
parser = self.protocol._reading_success_details.details_parser |
| 452 |
self.assertNotEqual(None, parser) |
| 453 |
self.assertIsInstance(parser, subunit.details.MultipartDetailsParser) |
| 454 |
|
| 455 |
|
| 456 |
class TestTestProtocolServerAddError(unittest.TestCase): |
| 457 |
def setUp(self): |
| 458 |
self.client = ExtendedTestResult() |
| 459 |
self.protocol = subunit.TestProtocolServer(self.client) |
| 460 |
self.protocol.lineReceived(b"test mcdonalds farm\n") |
| 461 |
self.test = subunit.RemotedTestCase("mcdonalds farm") |
| 462 |
|
| 463 |
def simple_error_keyword(self, keyword): |
| 464 |
self.protocol.lineReceived(("%s mcdonalds farm\n" % keyword).encode()) |
| 465 |
details = {} |
| 466 |
self.assertEqual( |
| 467 |
[ |
| 468 |
("startTest", self.test), |
| 469 |
("addError", self.test, details), |
| 470 |
("stopTest", self.test), |
| 471 |
], |
| 472 |
self.client._events, |
| 473 |
) |
| 474 |
|
| 475 |
def test_simple_error(self): |
| 476 |
self.simple_error_keyword("error") |
| 477 |
|
| 478 |
def test_simple_error_colon(self): |
| 479 |
self.simple_error_keyword("error:") |
| 480 |
|
| 481 |
def test_error_empty_message(self): |
| 482 |
self.protocol.lineReceived(b"error mcdonalds farm [\n") |
| 483 |
self.protocol.lineReceived(b"]\n") |
| 484 |
details = {} |
| 485 |
details["traceback"] = Content(ContentType("text", "x-traceback", {"charset": "utf8"}), lambda: [b""]) |
| 486 |
self.assertEqual( |
| 487 |
[ |
| 488 |
("startTest", self.test), |
| 489 |
("addError", self.test, details), |
| 490 |
("stopTest", self.test), |
| 491 |
], |
| 492 |
self.client._events, |
| 493 |
) |
| 494 |
|
| 495 |
def error_quoted_bracket(self, keyword): |
| 496 |
self.protocol.lineReceived(("%s mcdonalds farm [\n" % keyword).encode()) |
| 497 |
self.protocol.lineReceived(b" ]\n") |
| 498 |
self.protocol.lineReceived(b"]\n") |
| 499 |
details = {} |
| 500 |
details["traceback"] = Content(ContentType("text", "x-traceback", {"charset": "utf8"}), lambda: [b"]\n"]) |
| 501 |
self.assertEqual( |
| 502 |
[ |
| 503 |
("startTest", self.test), |
| 504 |
("addError", self.test, details), |
| 505 |
("stopTest", self.test), |
| 506 |
], |
| 507 |
self.client._events, |
| 508 |
) |
| 509 |
|
| 510 |
def test_error_quoted_bracket(self): |
| 511 |
self.error_quoted_bracket("error") |
| 512 |
|
| 513 |
def test_error_colon_quoted_bracket(self): |
| 514 |
self.error_quoted_bracket("error:") |
| 515 |
|
| 516 |
|
| 517 |
class TestTestProtocolServerAddFailure(unittest.TestCase): |
| 518 |
def setUp(self): |
| 519 |
self.client = ExtendedTestResult() |
| 520 |
self.protocol = subunit.TestProtocolServer(self.client) |
| 521 |
self.protocol.lineReceived(b"test mcdonalds farm\n") |
| 522 |
self.test = subunit.RemotedTestCase("mcdonalds farm") |
| 523 |
|
| 524 |
def assertFailure(self, details): |
| 525 |
self.assertEqual( |
| 526 |
[ |
| 527 |
("startTest", self.test), |
| 528 |
("addFailure", self.test, details), |
| 529 |
("stopTest", self.test), |
| 530 |
], |
| 531 |
self.client._events, |
| 532 |
) |
| 533 |
|
| 534 |
def simple_failure_keyword(self, keyword): |
| 535 |
self.protocol.lineReceived(("%s mcdonalds farm\n" % keyword).encode()) |
| 536 |
details = {} |
| 537 |
self.assertFailure(details) |
| 538 |
|
| 539 |
def test_simple_failure(self): |
| 540 |
self.simple_failure_keyword("failure") |
| 541 |
|
| 542 |
def test_simple_failure_colon(self): |
| 543 |
self.simple_failure_keyword("failure:") |
| 544 |
|
| 545 |
def test_failure_empty_message(self): |
| 546 |
self.protocol.lineReceived(b"failure mcdonalds farm [\n") |
| 547 |
self.protocol.lineReceived(b"]\n") |
| 548 |
details = {} |
| 549 |
details["traceback"] = Content(ContentType("text", "x-traceback", {"charset": "utf8"}), lambda: [b""]) |
| 550 |
self.assertFailure(details) |
| 551 |
|
| 552 |
def failure_quoted_bracket(self, keyword): |
| 553 |
self.protocol.lineReceived(("%s mcdonalds farm [\n" % keyword).encode()) |
| 554 |
self.protocol.lineReceived(b" ]\n") |
| 555 |
self.protocol.lineReceived(b"]\n") |
| 556 |
details = {} |
| 557 |
details["traceback"] = Content(ContentType("text", "x-traceback", {"charset": "utf8"}), lambda: [b"]\n"]) |
| 558 |
self.assertFailure(details) |
| 559 |
|
| 560 |
def test_failure_quoted_bracket(self): |
| 561 |
self.failure_quoted_bracket("failure") |
| 562 |
|
| 563 |
def test_failure_colon_quoted_bracket(self): |
| 564 |
self.failure_quoted_bracket("failure:") |
| 565 |
|
| 566 |
|
| 567 |
class TestTestProtocolServerAddxFail(unittest.TestCase): |
| 568 |
"""Tests for the xfail keyword.""" |
| 569 |
|
| 570 |
def setUp(self): |
| 571 |
"""Setup a test object ready to be xfailed with details.""" |
| 572 |
self.client = ExtendedTestResult() |
| 573 |
self.protocol = subunit.TestProtocolServer(self.client) |
| 574 |
self.protocol.lineReceived(b"test mcdonalds farm\n") |
| 575 |
self.test = self.client._events[-1][-1] |
| 576 |
|
| 577 |
def simple_xfail_keyword(self, keyword): |
| 578 |
self.protocol.lineReceived(("%s mcdonalds farm\n" % keyword).encode()) |
| 579 |
self.check_xfail() |
| 580 |
|
| 581 |
def check_xfail(self, error_message=None): |
| 582 |
details = {} |
| 583 |
if error_message is not None: |
| 584 |
details["traceback"] = Content( |
| 585 |
ContentType("text", "x-traceback", {"charset": "utf8"}), lambda: [error_message.encode()] |
| 586 |
) |
| 587 |
self.assertEqual( |
| 588 |
[ |
| 589 |
("startTest", self.test), |
| 590 |
("addExpectedFailure", self.test, details), |
| 591 |
("stopTest", self.test), |
| 592 |
], |
| 593 |
self.client._events, |
| 594 |
) |
| 595 |
|
| 596 |
def test_simple_xfail(self): |
| 597 |
self.simple_xfail_keyword("xfail") |
| 598 |
|
| 599 |
def test_simple_xfail_colon(self): |
| 600 |
self.simple_xfail_keyword("xfail:") |
| 601 |
|
| 602 |
def test_xfail_empty_message(self): |
| 603 |
self.protocol.lineReceived(b"xfail mcdonalds farm [\n") |
| 604 |
self.protocol.lineReceived(b"]\n") |
| 605 |
self.check_xfail(error_message="") |
| 606 |
|
| 607 |
def test_xfail_quoted_bracket(self): |
| 608 |
self.protocol.lineReceived(b"xfail mcdonalds farm [\n") |
| 609 |
self.protocol.lineReceived(b" ]\n") |
| 610 |
self.protocol.lineReceived(b"]\n") |
| 611 |
self.check_xfail("]\n") |
| 612 |
|
| 613 |
def test_xfail_colon_quoted_bracket(self): |
| 614 |
self.protocol.lineReceived(b"xfail: mcdonalds farm [\n") |
| 615 |
self.protocol.lineReceived(b" ]\n") |
| 616 |
self.protocol.lineReceived(b"]\n") |
| 617 |
self.check_xfail("]\n") |
| 618 |
|
| 619 |
|
| 620 |
class TestTestProtocolServerAddunexpectedSuccess(TestCase): |
| 621 |
"""Tests for the uxsuccess keyword.""" |
| 622 |
|
| 623 |
def setUp(self): |
| 624 |
"""Setup a test object ready for uxsuccess with details.""" |
| 625 |
super().setUp() |
| 626 |
self.client = ExtendedTestResult() |
| 627 |
self.protocol = subunit.TestProtocolServer(self.client) |
| 628 |
self.protocol.lineReceived(b"test mcdonalds farm\n") |
| 629 |
self.test = self.client._events[-1][-1] |
| 630 |
|
| 631 |
def simple_uxsuccess_keyword(self, keyword): |
| 632 |
self.protocol.lineReceived(("%s mcdonalds farm\n" % keyword).encode()) |
| 633 |
self.check_uxsuccess() |
| 634 |
|
| 635 |
def check_uxsuccess(self, error_message=None): |
| 636 |
if error_message is not None: |
| 637 |
details = {} |
| 638 |
details["traceback"] = Content( |
| 639 |
ContentType("text", "x-traceback", {"charset": "utf8"}), lambda: [error_message.encode()] |
| 640 |
) |
| 641 |
expected_events = [ |
| 642 |
("startTest", self.test), |
| 643 |
("addUnexpectedSuccess", self.test, details), |
| 644 |
("stopTest", self.test), |
| 645 |
] |
| 646 |
else: |
| 647 |
expected_events = [ |
| 648 |
("startTest", self.test), |
| 649 |
("addUnexpectedSuccess", self.test), |
| 650 |
("stopTest", self.test), |
| 651 |
] |
| 652 |
self.assertEqual(expected_events, self.client._events) |
| 653 |
|
| 654 |
def test_simple_uxsuccess(self): |
| 655 |
self.simple_uxsuccess_keyword("uxsuccess") |
| 656 |
|
| 657 |
def test_simple_uxsuccess_colon(self): |
| 658 |
self.simple_uxsuccess_keyword("uxsuccess:") |
| 659 |
|
| 660 |
def test_uxsuccess_empty_message(self): |
| 661 |
self.protocol.lineReceived(b"uxsuccess mcdonalds farm [\n") |
| 662 |
self.protocol.lineReceived(b"]\n") |
| 663 |
self.check_uxsuccess(error_message="") |
| 664 |
|
| 665 |
def test_uxsuccess_quoted_bracket(self): |
| 666 |
self.protocol.lineReceived(b"uxsuccess mcdonalds farm [\n") |
| 667 |
self.protocol.lineReceived(b" ]\n") |
| 668 |
self.protocol.lineReceived(b"]\n") |
| 669 |
self.check_uxsuccess("]\n") |
| 670 |
|
| 671 |
def test_uxsuccess_colon_quoted_bracket(self): |
| 672 |
self.protocol.lineReceived(b"uxsuccess: mcdonalds farm [\n") |
| 673 |
self.protocol.lineReceived(b" ]\n") |
| 674 |
self.protocol.lineReceived(b"]\n") |
| 675 |
self.check_uxsuccess("]\n") |
| 676 |
|
| 677 |
|
| 678 |
class TestTestProtocolServerAddSkip(unittest.TestCase): |
| 679 |
"""Tests for the skip keyword. |
| 680 |
|
| 681 |
In Python this meets the testtools extended TestResult contract. |
| 682 |
(See https://launchpad.net/testtools). |
| 683 |
""" |
| 684 |
|
| 685 |
def setUp(self): |
| 686 |
"""Setup a test object ready to be skipped.""" |
| 687 |
self.client = ExtendedTestResult() |
| 688 |
self.protocol = subunit.TestProtocolServer(self.client) |
| 689 |
self.protocol.lineReceived(b"test mcdonalds farm\n") |
| 690 |
self.test = self.client._events[-1][-1] |
| 691 |
|
| 692 |
def assertSkip(self, reason): |
| 693 |
details = {} |
| 694 |
if reason is not None: |
| 695 |
details["reason"] = Content(ContentType("text", "plain"), lambda: [reason]) |
| 696 |
self.assertEqual( |
| 697 |
[ |
| 698 |
("startTest", self.test), |
| 699 |
("addSkip", self.test, details), |
| 700 |
("stopTest", self.test), |
| 701 |
], |
| 702 |
self.client._events, |
| 703 |
) |
| 704 |
|
| 705 |
def simple_skip_keyword(self, keyword): |
| 706 |
self.protocol.lineReceived(("%s mcdonalds farm\n" % keyword).encode()) |
| 707 |
self.assertSkip(None) |
| 708 |
|
| 709 |
def test_simple_skip(self): |
| 710 |
self.simple_skip_keyword("skip") |
| 711 |
|
| 712 |
def test_simple_skip_colon(self): |
| 713 |
self.simple_skip_keyword("skip:") |
| 714 |
|
| 715 |
def test_skip_empty_message(self): |
| 716 |
self.protocol.lineReceived(b"skip mcdonalds farm [\n") |
| 717 |
self.protocol.lineReceived(b"]\n") |
| 718 |
self.assertSkip(b"") |
| 719 |
|
| 720 |
def skip_quoted_bracket(self, keyword): |
| 721 |
# This tests it is accepted, but cannot test it is used today, because |
| 722 |
# of not having a way to expose it in Python so far. |
| 723 |
self.protocol.lineReceived(("%s mcdonalds farm [\n" % keyword).encode()) |
| 724 |
self.protocol.lineReceived(b" ]\n") |
| 725 |
self.protocol.lineReceived(b"]\n") |
| 726 |
self.assertSkip(b"]\n") |
| 727 |
|
| 728 |
def test_skip_quoted_bracket(self): |
| 729 |
self.skip_quoted_bracket("skip") |
| 730 |
|
| 731 |
def test_skip_colon_quoted_bracket(self): |
| 732 |
self.skip_quoted_bracket("skip:") |
| 733 |
|
| 734 |
|
| 735 |
class TestTestProtocolServerAddSuccess(unittest.TestCase): |
| 736 |
def setUp(self): |
| 737 |
self.client = ExtendedTestResult() |
| 738 |
self.protocol = subunit.TestProtocolServer(self.client) |
| 739 |
self.protocol.lineReceived(b"test mcdonalds farm\n") |
| 740 |
self.test = subunit.RemotedTestCase("mcdonalds farm") |
| 741 |
|
| 742 |
def simple_success_keyword(self, keyword): |
| 743 |
self.protocol.lineReceived(("%s mcdonalds farm\n" % keyword).encode()) |
| 744 |
self.assertEqual( |
| 745 |
[ |
| 746 |
("startTest", self.test), |
| 747 |
("addSuccess", self.test), |
| 748 |
("stopTest", self.test), |
| 749 |
], |
| 750 |
self.client._events, |
| 751 |
) |
| 752 |
|
| 753 |
def test_simple_success(self): |
| 754 |
self.simple_success_keyword("successful") |
| 755 |
|
| 756 |
def test_simple_success_colon(self): |
| 757 |
self.simple_success_keyword("successful:") |
| 758 |
|
| 759 |
def assertSuccess(self, details): |
| 760 |
self.assertEqual( |
| 761 |
[ |
| 762 |
("startTest", self.test), |
| 763 |
("addSuccess", self.test, details), |
| 764 |
("stopTest", self.test), |
| 765 |
], |
| 766 |
self.client._events, |
| 767 |
) |
| 768 |
|
| 769 |
def test_success_empty_message(self): |
| 770 |
self.protocol.lineReceived(b"success mcdonalds farm [\n") |
| 771 |
self.protocol.lineReceived(b"]\n") |
| 772 |
details = {} |
| 773 |
details["message"] = Content(ContentType("text", "plain"), lambda: [b""]) |
| 774 |
self.assertSuccess(details) |
| 775 |
|
| 776 |
def success_quoted_bracket(self, keyword): |
| 777 |
# This tests it is accepted, but cannot test it is used today, because |
| 778 |
# of not having a way to expose it in Python so far. |
| 779 |
self.protocol.lineReceived(("%s mcdonalds farm [\n" % keyword).encode()) |
| 780 |
self.protocol.lineReceived(b" ]\n") |
| 781 |
self.protocol.lineReceived(b"]\n") |
| 782 |
details = {} |
| 783 |
details["message"] = Content(ContentType("text", "plain"), lambda: [b"]\n"]) |
| 784 |
self.assertSuccess(details) |
| 785 |
|
| 786 |
def test_success_quoted_bracket(self): |
| 787 |
self.success_quoted_bracket("success") |
| 788 |
|
| 789 |
def test_success_colon_quoted_bracket(self): |
| 790 |
self.success_quoted_bracket("success:") |
| 791 |
|
| 792 |
|
| 793 |
class TestTestProtocolServerProgress(unittest.TestCase): |
| 794 |
"""Test receipt of progress: directives.""" |
| 795 |
|
| 796 |
def test_progress_accepted_stdlib(self): |
| 797 |
self.result = ExtendedTestResult() |
| 798 |
self.stream = BytesIO() |
| 799 |
self.protocol = subunit.TestProtocolServer(self.result, stream=self.stream) |
| 800 |
self.protocol.lineReceived(b"progress: 23") |
| 801 |
self.protocol.lineReceived(b"progress: -2") |
| 802 |
self.protocol.lineReceived(b"progress: +4") |
| 803 |
self.assertEqual(b"", self.stream.getvalue()) |
| 804 |
|
| 805 |
def test_progress_accepted_extended(self): |
| 806 |
# With a progress capable TestResult, progress events are emitted. |
| 807 |
self.result = ExtendedTestResult() |
| 808 |
self.stream = BytesIO() |
| 809 |
self.protocol = subunit.TestProtocolServer(self.result, stream=self.stream) |
| 810 |
self.protocol.lineReceived(b"progress: 23") |
| 811 |
self.protocol.lineReceived(b"progress: push") |
| 812 |
self.protocol.lineReceived(b"progress: -2") |
| 813 |
self.protocol.lineReceived(b"progress: pop") |
| 814 |
self.protocol.lineReceived(b"progress: +4") |
| 815 |
self.assertEqual(b"", self.stream.getvalue()) |
| 816 |
self.assertEqual( |
| 817 |
[ |
| 818 |
("progress", 23, subunit.PROGRESS_SET), |
| 819 |
("progress", None, subunit.PROGRESS_PUSH), |
| 820 |
("progress", -2, subunit.PROGRESS_CUR), |
| 821 |
("progress", None, subunit.PROGRESS_POP), |
| 822 |
("progress", 4, subunit.PROGRESS_CUR), |
| 823 |
], |
| 824 |
self.result._events, |
| 825 |
) |
| 826 |
|
| 827 |
|
| 828 |
class TestTestProtocolServerStreamTags(unittest.TestCase): |
| 829 |
"""Test managing tags on the protocol level.""" |
| 830 |
|
| 831 |
def setUp(self): |
| 832 |
self.client = ExtendedTestResult() |
| 833 |
self.protocol = subunit.TestProtocolServer(self.client) |
| 834 |
|
| 835 |
def test_initial_tags(self): |
| 836 |
self.protocol.lineReceived(b"tags: foo bar:baz quux\n") |
| 837 |
self.assertEqual( |
| 838 |
[ |
| 839 |
("tags", {"foo", "bar:baz", "quux"}, set()), |
| 840 |
], |
| 841 |
self.client._events, |
| 842 |
) |
| 843 |
|
| 844 |
def test_minus_removes_tags(self): |
| 845 |
self.protocol.lineReceived(b"tags: -bar quux\n") |
| 846 |
self.assertEqual( |
| 847 |
[ |
| 848 |
("tags", {"quux"}, {"bar"}), |
| 849 |
], |
| 850 |
self.client._events, |
| 851 |
) |
| 852 |
|
| 853 |
def test_tags_do_not_get_set_on_test(self): |
| 854 |
self.protocol.lineReceived(b"test mcdonalds farm\n") |
| 855 |
test = self.client._events[0][-1] |
| 856 |
self.assertEqual(None, getattr(test, "tags", None)) |
| 857 |
|
| 858 |
def test_tags_do_not_get_set_on_global_tags(self): |
| 859 |
self.protocol.lineReceived(b"tags: foo bar\n") |
| 860 |
self.protocol.lineReceived(b"test mcdonalds farm\n") |
| 861 |
test = self.client._events[-1][-1] |
| 862 |
self.assertEqual(None, getattr(test, "tags", None)) |
| 863 |
|
| 864 |
def test_tags_get_set_on_test_tags(self): |
| 865 |
self.protocol.lineReceived(b"test mcdonalds farm\n") |
| 866 |
test = self.client._events[-1][-1] |
| 867 |
self.protocol.lineReceived(b"tags: foo bar\n") |
| 868 |
self.protocol.lineReceived(b"success mcdonalds farm\n") |
| 869 |
self.assertEqual(None, getattr(test, "tags", None)) |
| 870 |
|
| 871 |
|
| 872 |
class TestTestProtocolServerStreamTime(unittest.TestCase): |
| 873 |
"""Test managing time information at the protocol level.""" |
| 874 |
|
| 875 |
def test_time_accepted_stdlib(self): |
| 876 |
self.result = ExtendedTestResult() |
| 877 |
self.stream = BytesIO() |
| 878 |
self.protocol = subunit.TestProtocolServer(self.result, stream=self.stream) |
| 879 |
self.protocol.lineReceived(b"time: 2001-12-12 12:59:59Z\n") |
| 880 |
self.assertEqual(b"", self.stream.getvalue()) |
| 881 |
|
| 882 |
def test_time_accepted_extended(self): |
| 883 |
self.result = ExtendedTestResult() |
| 884 |
self.stream = BytesIO() |
| 885 |
self.protocol = subunit.TestProtocolServer(self.result, stream=self.stream) |
| 886 |
self.protocol.lineReceived(b"time: 2001-12-12 12:59:59Z\n") |
| 887 |
self.assertEqual(b"", self.stream.getvalue()) |
| 888 |
self.assertEqual([("time", datetime.datetime(2001, 12, 12, 12, 59, 59, 0, iso8601.UTC))], self.result._events) |
| 889 |
|
| 890 |
|
| 891 |
class TestRemotedTestCase(unittest.TestCase): |
| 892 |
def test_simple(self): |
| 893 |
test = subunit.RemotedTestCase("A test description") |
| 894 |
self.assertRaises(NotImplementedError, test.setUp) |
| 895 |
self.assertRaises(NotImplementedError, test.tearDown) |
| 896 |
self.assertEqual("A test description", test.shortDescription()) |
| 897 |
self.assertEqual("A test description", test.id()) |
| 898 |
self.assertEqual("A test description (subunit.RemotedTestCase)", "%s" % test) |
| 899 |
self.assertEqual("<subunit.RemotedTestCase description='A test description'>", "%r" % test) |
| 900 |
result = unittest.TestResult() |
| 901 |
test.run(result) |
| 902 |
self.assertEqual([(test, _remote_exception_repr + ": " + "Cannot run RemotedTestCases.\n\n")], result.errors) |
| 903 |
self.assertEqual(1, result.testsRun) |
| 904 |
another_test = subunit.RemotedTestCase("A test description") |
| 905 |
self.assertEqual(test, another_test) |
| 906 |
different_test = subunit.RemotedTestCase("ofo") |
| 907 |
self.assertNotEqual(test, different_test) |
| 908 |
self.assertNotEqual(another_test, different_test) |
| 909 |
|
| 910 |
|
| 911 |
class TestRemoteError(unittest.TestCase): |
| 912 |
def test_eq(self): |
| 913 |
error = subunit.RemoteError("Something went wrong") |
| 914 |
another_error = subunit.RemoteError("Something went wrong") |
| 915 |
different_error = subunit.RemoteError("boo!") |
| 916 |
self.assertEqual(error, another_error) |
| 917 |
self.assertNotEqual(error, different_error) |
| 918 |
self.assertNotEqual(different_error, another_error) |
| 919 |
|
| 920 |
def test_empty_constructor(self): |
| 921 |
self.assertEqual(subunit.RemoteError(), subunit.RemoteError("")) |
| 922 |
|
| 923 |
|
| 924 |
class TestExecTestCase(unittest.TestCase): |
| 925 |
class SampleExecTestCase(subunit.ExecTestCase): |
| 926 |
def test_sample_method(self): |
| 927 |
"""sample-script.py""" |
| 928 |
# the sample script runs three tests, one each |
| 929 |
# that fails, errors and succeeds |
| 930 |
|
| 931 |
def test_sample_method_args(self): |
| 932 |
"""sample-script.py foo""" |
| 933 |
# sample that will run just one test. |
| 934 |
|
| 935 |
def test_construct(self): |
| 936 |
test = self.SampleExecTestCase("test_sample_method") |
| 937 |
self.assertEqual(test.script, subunit.join_dir(__file__, "sample-script.py")) |
| 938 |
|
| 939 |
def test_args(self): |
| 940 |
result = unittest.TestResult() |
| 941 |
test = self.SampleExecTestCase("test_sample_method_args") |
| 942 |
test.run(result) |
| 943 |
self.assertEqual(1, result.testsRun) |
| 944 |
|
| 945 |
def test_run(self): |
| 946 |
result = ExtendedTestResult() |
| 947 |
test = self.SampleExecTestCase("test_sample_method") |
| 948 |
test.run(result) |
| 949 |
mcdonald = subunit.RemotedTestCase("old mcdonald") |
| 950 |
bing = subunit.RemotedTestCase("bing crosby") |
| 951 |
bing_details = {} |
| 952 |
bing_details["traceback"] = Content( |
| 953 |
ContentType("text", "x-traceback", {"charset": "utf8"}), lambda: [b"foo.c:53:ERROR invalid state\n"] |
| 954 |
) |
| 955 |
an_error = subunit.RemotedTestCase("an error") |
| 956 |
error_details = {} |
| 957 |
self.assertEqual( |
| 958 |
[ |
| 959 |
("startTest", mcdonald), |
| 960 |
("addSuccess", mcdonald), |
| 961 |
("stopTest", mcdonald), |
| 962 |
("startTest", bing), |
| 963 |
("addFailure", bing, bing_details), |
| 964 |
("stopTest", bing), |
| 965 |
("startTest", an_error), |
| 966 |
("addError", an_error, error_details), |
| 967 |
("stopTest", an_error), |
| 968 |
], |
| 969 |
result._events, |
| 970 |
) |
| 971 |
|
| 972 |
def test_debug(self): |
| 973 |
test = self.SampleExecTestCase("test_sample_method") |
| 974 |
test.debug() |
| 975 |
|
| 976 |
def test_count_test_cases(self): |
| 977 |
"""Run the child process and count responses to determine the count.""" |
| 978 |
|
| 979 |
def test_join_dir(self): |
| 980 |
sibling = subunit.join_dir(__file__, "foo") |
| 981 |
filedir = os.path.abspath(os.path.dirname(__file__)) |
| 982 |
expected = os.path.join(filedir, "foo") |
| 983 |
self.assertEqual(sibling, expected) |
| 984 |
|
| 985 |
|
| 986 |
class DoExecTestCase(subunit.ExecTestCase): |
| 987 |
def test_working_script(self): |
| 988 |
"""sample-two-script.py""" |
| 989 |
|
| 990 |
|
| 991 |
class TestIsolatedTestCase(TestCase): |
| 992 |
class SampleIsolatedTestCase(subunit.IsolatedTestCase): |
| 993 |
SETUP = False |
| 994 |
TEARDOWN = False |
| 995 |
TEST = False |
| 996 |
|
| 997 |
def setUp(self): |
| 998 |
TestIsolatedTestCase.SampleIsolatedTestCase.SETUP = True |
| 999 |
|
| 1000 |
def tearDown(self): |
| 1001 |
TestIsolatedTestCase.SampleIsolatedTestCase.TEARDOWN = True |
| 1002 |
|
| 1003 |
def test_sets_global_state(self): |
| 1004 |
TestIsolatedTestCase.SampleIsolatedTestCase.TEST = True |
| 1005 |
|
| 1006 |
def test_construct(self): |
| 1007 |
self.SampleIsolatedTestCase("test_sets_global_state") |
| 1008 |
|
| 1009 |
@skipIf(os.name != "posix", "Need a posix system for forking tests") |
| 1010 |
def test_run(self): |
| 1011 |
result = unittest.TestResult() |
| 1012 |
test = self.SampleIsolatedTestCase("test_sets_global_state") |
| 1013 |
test.run(result) |
| 1014 |
self.assertEqual(result.testsRun, 1) |
| 1015 |
self.assertEqual(self.SampleIsolatedTestCase.SETUP, False) |
| 1016 |
self.assertEqual(self.SampleIsolatedTestCase.TEARDOWN, False) |
| 1017 |
self.assertEqual(self.SampleIsolatedTestCase.TEST, False) |
| 1018 |
|
| 1019 |
def test_debug(self): |
| 1020 |
pass |
| 1021 |
# test = self.SampleExecTestCase("test_sample_method") |
| 1022 |
# test.debug() |
| 1023 |
|
| 1024 |
|
| 1025 |
class TestIsolatedTestSuite(TestCase): |
| 1026 |
class SampleTestToIsolate(unittest.TestCase): |
| 1027 |
SETUP = False |
| 1028 |
TEARDOWN = False |
| 1029 |
TEST = False |
| 1030 |
|
| 1031 |
def setUp(self): |
| 1032 |
TestIsolatedTestSuite.SampleTestToIsolate.SETUP = True |
| 1033 |
|
| 1034 |
def tearDown(self): |
| 1035 |
TestIsolatedTestSuite.SampleTestToIsolate.TEARDOWN = True |
| 1036 |
|
| 1037 |
def test_sets_global_state(self): |
| 1038 |
TestIsolatedTestSuite.SampleTestToIsolate.TEST = True |
| 1039 |
|
| 1040 |
def test_construct(self): |
| 1041 |
subunit.IsolatedTestSuite() |
| 1042 |
|
| 1043 |
@skipIf(os.name != "posix", "Need a posix system for forking tests") |
| 1044 |
def test_run(self): |
| 1045 |
result = unittest.TestResult() |
| 1046 |
suite = subunit.IsolatedTestSuite() |
| 1047 |
sub_suite = unittest.TestSuite() |
| 1048 |
sub_suite.addTest(self.SampleTestToIsolate("test_sets_global_state")) |
| 1049 |
sub_suite.addTest(self.SampleTestToIsolate("test_sets_global_state")) |
| 1050 |
suite.addTest(sub_suite) |
| 1051 |
suite.addTest(self.SampleTestToIsolate("test_sets_global_state")) |
| 1052 |
suite.run(result) |
| 1053 |
self.assertEqual(result.testsRun, 3) |
| 1054 |
self.assertEqual(self.SampleTestToIsolate.SETUP, False) |
| 1055 |
self.assertEqual(self.SampleTestToIsolate.TEARDOWN, False) |
| 1056 |
self.assertEqual(self.SampleTestToIsolate.TEST, False) |
| 1057 |
|
| 1058 |
|
| 1059 |
# A number of these tests produce different output depending on the |
| 1060 |
# testtools version. testtools < 2.5.0 used traceback2, which incorrectly |
| 1061 |
# included the traceback header even for an exception with no traceback. |
| 1062 |
# testtools 2.5.0 switched to the Python 3 standard library's traceback |
| 1063 |
# module, which fixes this bug. See https://bugs.python.org/issue24695. |
| 1064 |
class TestTestProtocolClient(TestCase): |
| 1065 |
def setUp(self): |
| 1066 |
super().setUp() |
| 1067 |
self.io = BytesIO() |
| 1068 |
self.protocol = subunit.TestProtocolClient(self.io) |
| 1069 |
self.unicode_test = PlaceHolder("\u2603") |
| 1070 |
self.test = TestTestProtocolClient("test_start_test") |
| 1071 |
self.sample_details = {"something": Content(ContentType("text", "plain"), lambda: [b"serialised\nform"])} |
| 1072 |
self.sample_tb_details = dict(self.sample_details) |
| 1073 |
self.sample_tb_details["traceback"] = TracebackContent(subunit.RemoteError("boo qux"), self.test) |
| 1074 |
|
| 1075 |
def test_start_test(self): |
| 1076 |
"""Test startTest on a TestProtocolClient.""" |
| 1077 |
self.protocol.startTest(self.test) |
| 1078 |
self.assertEqual(self.io.getvalue(), ("test: %s\n" % self.test.id()).encode()) |
| 1079 |
|
| 1080 |
def test_start_test_unicode_id(self): |
| 1081 |
"""Test startTest on a TestProtocolClient.""" |
| 1082 |
self.protocol.startTest(self.unicode_test) |
| 1083 |
expected = b"test: " + "\u2603".encode("utf8") + b"\n" |
| 1084 |
self.assertEqual(expected, self.io.getvalue()) |
| 1085 |
|
| 1086 |
def test_stop_test(self): |
| 1087 |
# stopTest doesn't output anything. |
| 1088 |
self.protocol.stopTest(self.test) |
| 1089 |
self.assertEqual(self.io.getvalue(), b"") |
| 1090 |
|
| 1091 |
def test_add_success(self): |
| 1092 |
"""Test addSuccess on a TestProtocolClient.""" |
| 1093 |
self.protocol.addSuccess(self.test) |
| 1094 |
self.assertEqual(self.io.getvalue(), ("successful: %s\n" % self.test.id()).encode()) |
| 1095 |
|
| 1096 |
def test_add_outcome_unicode_id(self): |
| 1097 |
"""Test addSuccess on a TestProtocolClient.""" |
| 1098 |
self.protocol.addSuccess(self.unicode_test) |
| 1099 |
expected = b"successful: " + "\u2603".encode("utf8") + b"\n" |
| 1100 |
self.assertEqual(expected, self.io.getvalue()) |
| 1101 |
|
| 1102 |
def test_add_success_details(self): |
| 1103 |
"""Test addSuccess on a TestProtocolClient with details.""" |
| 1104 |
self.protocol.addSuccess(self.test, details=self.sample_details) |
| 1105 |
self.assertEqual( |
| 1106 |
self.io.getvalue(), |
| 1107 |
( |
| 1108 |
"successful: %s [ multipart\n" |
| 1109 |
"Content-Type: text/plain\n" |
| 1110 |
"something\n" |
| 1111 |
"F\r\nserialised\nform0\r\n]\n" % self.test.id() |
| 1112 |
).encode(), |
| 1113 |
) |
| 1114 |
|
| 1115 |
def test_add_failure(self): |
| 1116 |
"""Test addFailure on a TestProtocolClient.""" |
| 1117 |
self.protocol.addFailure(self.test, subunit.RemoteError("boo qux")) |
| 1118 |
self.assertThat( |
| 1119 |
self.io.getvalue(), |
| 1120 |
MatchesAny( |
| 1121 |
# testtools < 2.5.0 |
| 1122 |
Equals((("failure: %s [\n" + _remote_exception_str + ": boo qux\n" + "]\n") % self.test.id()).encode()), |
| 1123 |
# testtools >= 2.5.0 |
| 1124 |
Equals( |
| 1125 |
(("failure: %s [\n" + _remote_exception_repr + ": boo qux\n" + "]\n") % self.test.id()).encode() |
| 1126 |
), |
| 1127 |
), |
| 1128 |
) |
| 1129 |
|
| 1130 |
def test_add_failure_details(self): |
| 1131 |
"""Test addFailure on a TestProtocolClient with details.""" |
| 1132 |
self.protocol.addFailure(self.test, details=self.sample_tb_details) |
| 1133 |
self.assertThat( |
| 1134 |
self.io.getvalue(), |
| 1135 |
MatchesAny( |
| 1136 |
# testtools < 2.5.0 |
| 1137 |
Equals( |
| 1138 |
( |
| 1139 |
( |
| 1140 |
"failure: %s [ multipart\n" |
| 1141 |
"Content-Type: text/plain\n" |
| 1142 |
"something\n" |
| 1143 |
"F\r\nserialised\nform0\r\n" |
| 1144 |
"Content-Type: text/x-traceback;charset=utf8,language=python\n" |
| 1145 |
"traceback\n" + _remote_exception_str_chunked + "]\n" |
| 1146 |
) |
| 1147 |
% self.test.id() |
| 1148 |
).encode() |
| 1149 |
), |
| 1150 |
# testtools >= 2.5.0 |
| 1151 |
Equals( |
| 1152 |
( |
| 1153 |
( |
| 1154 |
"failure: %s [ multipart\n" |
| 1155 |
"Content-Type: text/plain\n" |
| 1156 |
"something\n" |
| 1157 |
"F\r\nserialised\nform0\r\n" |
| 1158 |
"Content-Type: text/x-traceback;charset=utf8,language=python\n" |
| 1159 |
"traceback\n" + _remote_exception_repr_chunked + "]\n" |
| 1160 |
) |
| 1161 |
% self.test.id() |
| 1162 |
).encode() |
| 1163 |
), |
| 1164 |
), |
| 1165 |
) |
| 1166 |
|
| 1167 |
def test_add_error(self): |
| 1168 |
"""Test stopTest on a TestProtocolClient.""" |
| 1169 |
self.protocol.addError(self.test, subunit.RemoteError("phwoar crikey")) |
| 1170 |
self.assertThat( |
| 1171 |
self.io.getvalue(), |
| 1172 |
MatchesAny( |
| 1173 |
# testtools < 2.5.0 |
| 1174 |
Equals((("error: %s [\n" + _remote_exception_str + ": phwoar crikey\n]\n") % self.test.id()).encode()), |
| 1175 |
# testtools >= 2.5.0 |
| 1176 |
Equals((("error: %s [\n" + _remote_exception_repr + ": phwoar crikey\n]\n") % self.test.id()).encode()), |
| 1177 |
), |
| 1178 |
) |
| 1179 |
|
| 1180 |
def test_add_error_details(self): |
| 1181 |
"""Test stopTest on a TestProtocolClient with details.""" |
| 1182 |
self.protocol.addError(self.test, details=self.sample_tb_details) |
| 1183 |
self.assertThat( |
| 1184 |
self.io.getvalue(), |
| 1185 |
MatchesAny( |
| 1186 |
# testtools < 2.5.0 |
| 1187 |
Equals( |
| 1188 |
( |
| 1189 |
( |
| 1190 |
"error: %s [ multipart\n" |
| 1191 |
"Content-Type: text/plain\n" |
| 1192 |
"something\n" |
| 1193 |
"F\r\nserialised\nform0\r\n" |
| 1194 |
"Content-Type: text/x-traceback;charset=utf8,language=python\n" |
| 1195 |
"traceback\n" + _remote_exception_str_chunked + "]\n" |
| 1196 |
) |
| 1197 |
% self.test.id() |
| 1198 |
).encode() |
| 1199 |
), |
| 1200 |
# testtools >= 2.5.0 |
| 1201 |
Equals( |
| 1202 |
( |
| 1203 |
( |
| 1204 |
"error: %s [ multipart\n" |
| 1205 |
"Content-Type: text/plain\n" |
| 1206 |
"something\n" |
| 1207 |
"F\r\nserialised\nform0\r\n" |
| 1208 |
"Content-Type: text/x-traceback;charset=utf8,language=python\n" |
| 1209 |
"traceback\n" + _remote_exception_repr_chunked + "]\n" |
| 1210 |
) |
| 1211 |
% self.test.id() |
| 1212 |
).encode() |
| 1213 |
), |
| 1214 |
), |
| 1215 |
) |
| 1216 |
|
| 1217 |
def test_add_expected_failure(self): |
| 1218 |
"""Test addExpectedFailure on a TestProtocolClient.""" |
| 1219 |
self.protocol.addExpectedFailure(self.test, subunit.RemoteError("phwoar crikey")) |
| 1220 |
self.assertThat( |
| 1221 |
self.io.getvalue(), |
| 1222 |
MatchesAny( |
| 1223 |
# testtools < 2.5.0 |
| 1224 |
Equals((("xfail: %s [\n" + _remote_exception_str + ": phwoar crikey\n]\n") % self.test.id()).encode()), |
| 1225 |
# testtools >= 2.5.0 |
| 1226 |
Equals((("xfail: %s [\n" + _remote_exception_repr + ": phwoar crikey\n]\n") % self.test.id()).encode()), |
| 1227 |
), |
| 1228 |
) |
| 1229 |
|
| 1230 |
def test_add_expected_failure_details(self): |
| 1231 |
"""Test addExpectedFailure on a TestProtocolClient with details.""" |
| 1232 |
self.protocol.addExpectedFailure(self.test, details=self.sample_tb_details) |
| 1233 |
self.assertThat( |
| 1234 |
self.io.getvalue(), |
| 1235 |
MatchesAny( |
| 1236 |
# testtools < 2.5.0 |
| 1237 |
Equals( |
| 1238 |
( |
| 1239 |
( |
| 1240 |
"xfail: %s [ multipart\n" |
| 1241 |
"Content-Type: text/plain\n" |
| 1242 |
"something\n" |
| 1243 |
"F\r\nserialised\nform0\r\n" |
| 1244 |
"Content-Type: text/x-traceback;charset=utf8,language=python\n" |
| 1245 |
"traceback\n" + _remote_exception_str_chunked + "]\n" |
| 1246 |
) |
| 1247 |
% self.test.id() |
| 1248 |
).encode() |
| 1249 |
), |
| 1250 |
# testtools >= 2.5.0 |
| 1251 |
Equals( |
| 1252 |
( |
| 1253 |
( |
| 1254 |
"xfail: %s [ multipart\n" |
| 1255 |
"Content-Type: text/plain\n" |
| 1256 |
"something\n" |
| 1257 |
"F\r\nserialised\nform0\r\n" |
| 1258 |
"Content-Type: text/x-traceback;charset=utf8,language=python\n" |
| 1259 |
"traceback\n" + _remote_exception_repr_chunked + "]\n" |
| 1260 |
) |
| 1261 |
% self.test.id() |
| 1262 |
).encode() |
| 1263 |
), |
| 1264 |
), |
| 1265 |
) |
| 1266 |
|
| 1267 |
def test_add_skip(self): |
| 1268 |
"""Test addSkip on a TestProtocolClient.""" |
| 1269 |
self.protocol.addSkip(self.test, "Has it really?") |
| 1270 |
self.assertEqual(self.io.getvalue(), ("skip: %s [\nHas it really?\n]\n" % self.test.id()).encode()) |
| 1271 |
|
| 1272 |
def test_add_skip_details(self): |
| 1273 |
"""Test addSkip on a TestProtocolClient with details.""" |
| 1274 |
details = {"reason": Content(ContentType("text", "plain"), lambda: [b"Has it really?"])} |
| 1275 |
self.protocol.addSkip(self.test, details=details) |
| 1276 |
self.assertEqual( |
| 1277 |
self.io.getvalue(), |
| 1278 |
( |
| 1279 |
"skip: %s [ multipart\nContent-Type: text/plain\nreason\nE\r\nHas it really?0\r\n]\n" % self.test.id() |
| 1280 |
).encode(), |
| 1281 |
) |
| 1282 |
|
| 1283 |
def test_progress_set(self): |
| 1284 |
self.protocol.progress(23, subunit.PROGRESS_SET) |
| 1285 |
self.assertEqual(self.io.getvalue(), b"progress: 23\n") |
| 1286 |
|
| 1287 |
def test_progress_neg_cur(self): |
| 1288 |
self.protocol.progress(-23, subunit.PROGRESS_CUR) |
| 1289 |
self.assertEqual(self.io.getvalue(), b"progress: -23\n") |
| 1290 |
|
| 1291 |
def test_progress_pos_cur(self): |
| 1292 |
self.protocol.progress(23, subunit.PROGRESS_CUR) |
| 1293 |
self.assertEqual(self.io.getvalue(), b"progress: +23\n") |
| 1294 |
|
| 1295 |
def test_progress_pop(self): |
| 1296 |
self.protocol.progress(1234, subunit.PROGRESS_POP) |
| 1297 |
self.assertEqual(self.io.getvalue(), b"progress: pop\n") |
| 1298 |
|
| 1299 |
def test_progress_push(self): |
| 1300 |
self.protocol.progress(1234, subunit.PROGRESS_PUSH) |
| 1301 |
self.assertEqual(self.io.getvalue(), b"progress: push\n") |
| 1302 |
|
| 1303 |
def test_time(self): |
| 1304 |
# Calling time() outputs a time signal immediately. |
| 1305 |
self.protocol.time(datetime.datetime(2009, 10, 11, 12, 13, 14, 15, iso8601.UTC)) |
| 1306 |
self.assertEqual(b"time: 2009-10-11 12:13:14.000015Z\n", self.io.getvalue()) |
| 1307 |
|
| 1308 |
def test_add_unexpected_success(self): |
| 1309 |
"""Test addUnexpectedSuccess on a TestProtocolClient.""" |
| 1310 |
self.protocol.addUnexpectedSuccess(self.test) |
| 1311 |
self.assertEqual(self.io.getvalue(), ("uxsuccess: %s\n" % self.test.id()).encode()) |
| 1312 |
|
| 1313 |
def test_add_unexpected_success_details(self): |
| 1314 |
"""Test addUnexpectedSuccess on a TestProtocolClient with details.""" |
| 1315 |
self.protocol.addUnexpectedSuccess(self.test, details=self.sample_details) |
| 1316 |
self.assertEqual( |
| 1317 |
self.io.getvalue(), |
| 1318 |
( |
| 1319 |
"uxsuccess: %s [ multipart\n" |
| 1320 |
"Content-Type: text/plain\n" |
| 1321 |
"something\n" |
| 1322 |
"F\r\nserialised\nform0\r\n]\n" % self.test.id() |
| 1323 |
).encode(), |
| 1324 |
) |
| 1325 |
|
| 1326 |
def test_tags_empty(self): |
| 1327 |
self.protocol.tags(set(), set()) |
| 1328 |
self.assertEqual(b"", self.io.getvalue()) |
| 1329 |
|
| 1330 |
def test_tags_add(self): |
| 1331 |
self.protocol.tags({"foo"}, set()) |
| 1332 |
self.assertEqual(b"tags: foo\n", self.io.getvalue()) |
| 1333 |
|
| 1334 |
def test_tags_both(self): |
| 1335 |
self.protocol.tags({"quux"}, {"bar"}) |
| 1336 |
self.assertThat([b"tags: quux -bar\n", b"tags: -bar quux\n"], Contains(self.io.getvalue())) |
| 1337 |
|
| 1338 |
def test_tags_gone(self): |
| 1339 |
self.protocol.tags(set(), {"bar"}) |
| 1340 |
self.assertEqual(b"tags: -bar\n", self.io.getvalue()) |