test.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. import os
  4. from Naked.toolshed.system import cwd, file_exists, dir_exists, stderr, exit_success
  5. #------------------------------------------------------------------------------
  6. # [ ToxTester class ]
  7. # Run Tox on the project directory, by default runs all python versions in tox.ini
  8. # Optional specify the version of Python to test in constructor (runs 'tox -e py<version>')
  9. # Optional specify the number of directory levels `dir_levels` to search bottom to top (default = 4)
  10. #------------------------------------------------------------------------------
  11. class ToxTester:
  12. def __init__(self, py_version="", dir_levels = 6):
  13. self.py_version = py_version
  14. self.number_of_dir_levels = dir_levels
  15. def run(self):
  16. tox_found = False
  17. for i in range(self.number_of_dir_levels):
  18. if not self._is_tox_ini_at_this_level():
  19. os.chdir(os.pardir)
  20. else:
  21. tox_found = True
  22. self._run_tox()
  23. break
  24. if not tox_found:
  25. stderr("Unable to locate your tox.ini file. Please navigate to your project directory.", 1)
  26. else:
  27. exit_success()
  28. def _is_tox_ini_at_this_level(self):
  29. if file_exists('tox.ini'):
  30. return True
  31. else:
  32. return False
  33. def _run_tox(self):
  34. if self.py_version == "":
  35. os.system("tox")
  36. else:
  37. cmd_string = "tox -e" + self.py_version
  38. os.system(cmd_string)
  39. #------------------------------------------------------------------------------
  40. # [ NoseTester class ]
  41. # run nose tests from the tests directory (works from any level of the project)
  42. # Optional specify the number of directory levels to search bottom to top (default = 4)
  43. #------------------------------------------------------------------------------
  44. class NoseTester:
  45. def __init__(self, dir_levels = 6):
  46. self.number_of_dir_levels = dir_levels
  47. def run(self):
  48. nose_found = False
  49. for i in range(self.number_of_dir_levels):
  50. if not self._is_testdir_at_this_level():
  51. os.chdir(os.pardir)
  52. else:
  53. nose_found = True
  54. self._run_nose()
  55. break
  56. if not nose_found:
  57. stderr("Unable to locate your testing directory", 1)
  58. else:
  59. exit_success()
  60. def _is_testdir_at_this_level(self):
  61. if file_exists('setup.py'):
  62. if dir_exists('tests'):
  63. return True
  64. else:
  65. return False #found setup.py but no tests directory
  66. else:
  67. return False # setup.py not at this level
  68. def _run_nose(self):
  69. os.system("nosetests --where=tests")
  70. #------------------------------------------------------------------------------
  71. # [ PyTester class ]
  72. # run py.test test runner in the tests directory
  73. # Optional specify the number of directory levels to search bottom to top (default = 4)
  74. #------------------------------------------------------------------------------
  75. class PyTester:
  76. def __init__(self, dir_levels = 6):
  77. self.number_of_dir_levels = dir_levels
  78. def run(self):
  79. py_found = False
  80. for i in range(self.number_of_dir_levels):
  81. if not self._is_testdir_at_this_level():
  82. os.chdir(os.pardir)
  83. else:
  84. py_found = True
  85. self._run_pytests()
  86. break
  87. if not py_found:
  88. stderr("Unable to locate your testing directory", 1)
  89. else:
  90. exit_success()
  91. def _is_testdir_at_this_level(self):
  92. if file_exists('setup.py'):
  93. if dir_exists('tests'):
  94. return True
  95. else:
  96. return False
  97. else:
  98. return False
  99. def _run_pytests(self):
  100. os.chdir('tests')
  101. os.system('py.test')
  102. #------------------------------------------------------------------------------
  103. # [ UnitTester class ]
  104. # run Python unit tests with the built in unittest methods
  105. # Optional specify the number of directory levels to search bottom to top (default = 4)
  106. #------------------------------------------------------------------------------
  107. class UnitTester:
  108. def __init__(self, the_unit_test, dir_levels = 6):
  109. self.unittest = the_unit_test
  110. self.number_of_dir_levels = dir_levels
  111. def run(self):
  112. unit_found = False
  113. for i in range(self.number_of_dir_levels):
  114. if not self._is_testdir_at_this_level():
  115. os.chdir(os.pardir)
  116. else:
  117. unit_found = True
  118. os.chdir('tests')
  119. if file_exists(self.unittest):
  120. self._run_unittest()
  121. else:
  122. stderr("The unit test file " + self.unittest + " could not be found in the tests directory.")
  123. if not unit_found:
  124. stderr("Unable to locate your testing directory", 1)
  125. else:
  126. exit_success()
  127. def _is_testdir_at_this_level(self):
  128. if file_exists('setup.py'):
  129. if dir_exists('tests'):
  130. return True
  131. else:
  132. return False
  133. else:
  134. return False
  135. def _run_unittest(self):
  136. cmd_string = "python " + self.unittest
  137. os.system(cmd_string)
  138. def help():
  139. help_string = """
  140. Naked test Command Help
  141. =======================
  142. The test command allows you to run unit tests from any working directory in your project.
  143. USAGE
  144. naked test <secondary command> [argument]
  145. SECONDARY COMMANDS
  146. nose - run the nose test runner on your project
  147. pytest - run the py.test test runner on your project
  148. tox - run the tox test runner on your project
  149. unittest - run Python unit tests (built-in)
  150. ARGUMENTS
  151. nose
  152. -- does not take additional arguments
  153. pytest
  154. -- does not take additional arguments
  155. tox [python version]
  156. -- You can include an optional tox Python version argument to run your
  157. tests with a single version of Python (instead of the versions
  158. specified in the tox.ini file). By default, the versions specified
  159. in your tox.ini file are run.
  160. unittest <test file>
  161. -- Mandatory unit test file path (relative to the tests directory)
  162. OPTIONS
  163. none
  164. EXAMPLES
  165. naked test nose
  166. naked test pytest
  167. naked test tox
  168. naked test tox py27
  169. naked test unittest test_app.py
  170. A bottom to top search (from the working directory) is performed over up to 6 directory levels to find the 'tests' directory."""
  171. print(help_string)
  172. exit_success()
  173. if __name__ == '__main__':
  174. pass