test_parallel.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import unittest
  2. from django.test import SimpleTestCase
  3. from django.test.runner import RemoteTestResult
  4. try:
  5. import tblib
  6. except ImportError:
  7. tblib = None
  8. class ExceptionThatFailsUnpickling(Exception):
  9. """
  10. After pickling, this class fails unpickling with an error about incorrect
  11. arguments passed to __init__().
  12. """
  13. def __init__(self, arg):
  14. super().__init__()
  15. class ParallelTestRunnerTest(SimpleTestCase):
  16. """
  17. End-to-end tests of the parallel test runner.
  18. These tests are only meaningful when running tests in parallel using
  19. the --parallel option, though it doesn't hurt to run them not in
  20. parallel.
  21. """
  22. def test_subtest(self):
  23. """
  24. Passing subtests work.
  25. """
  26. for i in range(2):
  27. with self.subTest(index=i):
  28. self.assertEqual(i, i)
  29. class SampleFailingSubtest(SimpleTestCase):
  30. # This method name doesn't begin with "test" to prevent test discovery
  31. # from seeing it.
  32. def dummy_test(self):
  33. """
  34. A dummy test for testing subTest failures.
  35. """
  36. for i in range(3):
  37. with self.subTest(index=i):
  38. self.assertEqual(i, 1)
  39. class RemoteTestResultTest(SimpleTestCase):
  40. def test_pickle_errors_detection(self):
  41. picklable_error = RuntimeError('This is fine')
  42. not_unpicklable_error = ExceptionThatFailsUnpickling('arg')
  43. result = RemoteTestResult()
  44. result._confirm_picklable(picklable_error)
  45. msg = '__init__() missing 1 required positional argument'
  46. with self.assertRaisesMessage(TypeError, msg):
  47. result._confirm_picklable(not_unpicklable_error)
  48. @unittest.skipUnless(tblib is not None, 'requires tblib to be installed')
  49. def test_add_failing_subtests(self):
  50. """
  51. Failing subtests are added correctly using addSubTest().
  52. """
  53. # Manually run a test with failing subtests to prevent the failures
  54. # from affecting the actual test run.
  55. result = RemoteTestResult()
  56. subtest_test = SampleFailingSubtest(methodName='dummy_test')
  57. subtest_test.run(result=result)
  58. events = result.events
  59. self.assertEqual(len(events), 4)
  60. event = events[1]
  61. self.assertEqual(event[0], 'addSubTest')
  62. self.assertEqual(str(event[2]), 'dummy_test (test_runner.test_parallel.SampleFailingSubtest) (index=0)')
  63. self.assertEqual(repr(event[3][1]), "AssertionError('0 != 1')")
  64. event = events[2]
  65. self.assertEqual(repr(event[3][1]), "AssertionError('2 != 1')")