test_output.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for the output generated by trial.
  5. """
  6. from __future__ import absolute_import, division, print_function
  7. import os
  8. from twisted.python.compat import NativeStringIO, _PY3
  9. from twisted.scripts import trial
  10. from twisted.trial import runner
  11. from twisted.trial.test import packages
  12. if _PY3:
  13. _noModuleError = "No module named 'frotz'"
  14. else:
  15. _noModuleError = "No module named frotz"
  16. def runTrial(*args):
  17. from twisted.trial import reporter
  18. config = trial.Options()
  19. config.parseOptions(args)
  20. output = NativeStringIO()
  21. myRunner = runner.TrialRunner(
  22. reporter.VerboseTextReporter,
  23. stream=output,
  24. workingDirectory=config['temp-directory'])
  25. suite = trial._getSuite(config)
  26. myRunner.run(suite)
  27. return output.getvalue()
  28. class ImportErrorsTests(packages.SysPathManglingTest):
  29. """Actually run trial as if on the command line and check that the output
  30. is what we expect.
  31. """
  32. debug = False
  33. parent = "_testImportErrors"
  34. def runTrial(self, *args):
  35. return runTrial('--temp-directory', self.mktemp(), *args)
  36. def _print(self, stuff):
  37. print(stuff)
  38. return stuff
  39. def assertIn(self, container, containee, *args, **kwargs):
  40. # redefined to be useful in callbacks
  41. super(ImportErrorsTests, self).assertIn(
  42. containee, container, *args, **kwargs)
  43. return container
  44. def assertNotIn(self, container, containee, *args, **kwargs):
  45. # redefined to be useful in callbacks
  46. super(ImportErrorsTests, self).assertNotIn(
  47. containee, container, *args, **kwargs)
  48. return container
  49. def test_trialRun(self):
  50. self.runTrial()
  51. def test_nonexistentModule(self):
  52. d = self.runTrial('twisted.doesntexist')
  53. self.assertIn(d, '[ERROR]')
  54. self.assertIn(d, 'twisted.doesntexist')
  55. return d
  56. def test_nonexistentPackage(self):
  57. d = self.runTrial('doesntexist')
  58. self.assertIn(d, 'doesntexist')
  59. self.assertIn(d, 'ModuleNotFound')
  60. self.assertIn(d, '[ERROR]')
  61. return d
  62. def test_nonexistentPackageWithModule(self):
  63. d = self.runTrial('doesntexist.barney')
  64. self.assertIn(d, 'doesntexist.barney')
  65. self.assertIn(d, 'ObjectNotFound')
  66. self.assertIn(d, '[ERROR]')
  67. return d
  68. def test_badpackage(self):
  69. d = self.runTrial('badpackage')
  70. self.assertIn(d, '[ERROR]')
  71. self.assertIn(d, 'badpackage')
  72. self.assertNotIn(d, 'IOError')
  73. return d
  74. def test_moduleInBadpackage(self):
  75. d = self.runTrial('badpackage.test_module')
  76. self.assertIn(d, "[ERROR]")
  77. self.assertIn(d, "badpackage.test_module")
  78. self.assertNotIn(d, 'IOError')
  79. return d
  80. def test_badmodule(self):
  81. d = self.runTrial('package.test_bad_module')
  82. self.assertIn(d, '[ERROR]')
  83. self.assertIn(d, 'package.test_bad_module')
  84. self.assertNotIn(d, 'IOError')
  85. self.assertNotIn(d, '<module ')
  86. return d
  87. def test_badimport(self):
  88. d = self.runTrial('package.test_import_module')
  89. self.assertIn(d, '[ERROR]')
  90. self.assertIn(d, 'package.test_import_module')
  91. self.assertNotIn(d, 'IOError')
  92. self.assertNotIn(d, '<module ')
  93. return d
  94. def test_recurseImport(self):
  95. d = self.runTrial('package')
  96. self.assertIn(d, '[ERROR]')
  97. self.assertIn(d, 'test_bad_module')
  98. self.assertIn(d, 'test_import_module')
  99. self.assertNotIn(d, '<module ')
  100. self.assertNotIn(d, 'IOError')
  101. return d
  102. def test_recurseImportErrors(self):
  103. d = self.runTrial('package2')
  104. self.assertIn(d, '[ERROR]')
  105. self.assertIn(d, 'package2')
  106. self.assertIn(d, 'test_module')
  107. self.assertIn(d, _noModuleError)
  108. self.assertNotIn(d, '<module ')
  109. self.assertNotIn(d, 'IOError')
  110. return d
  111. def test_nonRecurseImportErrors(self):
  112. d = self.runTrial('-N', 'package2')
  113. self.assertIn(d, '[ERROR]')
  114. self.assertIn(d, _noModuleError)
  115. self.assertNotIn(d, '<module ')
  116. return d
  117. def test_regularRun(self):
  118. d = self.runTrial('package.test_module')
  119. self.assertNotIn(d, '[ERROR]')
  120. self.assertNotIn(d, 'IOError')
  121. self.assertIn(d, 'OK')
  122. self.assertIn(d, 'PASSED (successes=1)')
  123. return d
  124. def test_filename(self):
  125. self.mangleSysPath(self.oldPath)
  126. d = self.runTrial(
  127. os.path.join(self.parent, 'package', 'test_module.py'))
  128. self.assertNotIn(d, '[ERROR]')
  129. self.assertNotIn(d, 'IOError')
  130. self.assertIn(d, 'OK')
  131. self.assertIn(d, 'PASSED (successes=1)')
  132. return d
  133. def test_dosFile(self):
  134. ## XXX -- not really an output test, more of a script test
  135. self.mangleSysPath(self.oldPath)
  136. d = self.runTrial(
  137. os.path.join(self.parent,
  138. 'package', 'test_dos_module.py'))
  139. self.assertNotIn(d, '[ERROR]')
  140. self.assertNotIn(d, 'IOError')
  141. self.assertIn(d, 'OK')
  142. self.assertIn(d, 'PASSED (successes=1)')
  143. return d