test_test.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import mock
  2. from distutils import log
  3. import os
  4. import pytest
  5. from setuptools.command.test import test
  6. from setuptools.dist import Distribution
  7. from setuptools.tests import ack_2to3
  8. from .textwrap import DALS
  9. SETUP_PY = DALS("""
  10. from setuptools import setup
  11. setup(name='foo',
  12. packages=['name', 'name.space', 'name.space.tests'],
  13. namespace_packages=['name'],
  14. test_suite='name.space.tests.test_suite',
  15. )
  16. """)
  17. NS_INIT = DALS("""
  18. # -*- coding: Latin-1 -*-
  19. # Söme Arbiträry Ünicode to test Distribute Issüé 310
  20. try:
  21. __import__('pkg_resources').declare_namespace(__name__)
  22. except ImportError:
  23. from pkgutil import extend_path
  24. __path__ = extend_path(__path__, __name__)
  25. """)
  26. TEST_PY = DALS("""
  27. import unittest
  28. class TestTest(unittest.TestCase):
  29. def test_test(self):
  30. print "Foo" # Should fail under Python 3 unless 2to3 is used
  31. test_suite = unittest.makeSuite(TestTest)
  32. """)
  33. @pytest.fixture
  34. def sample_test(tmpdir_cwd):
  35. os.makedirs('name/space/tests')
  36. # setup.py
  37. with open('setup.py', 'wt') as f:
  38. f.write(SETUP_PY)
  39. # name/__init__.py
  40. with open('name/__init__.py', 'wb') as f:
  41. f.write(NS_INIT.encode('Latin-1'))
  42. # name/space/__init__.py
  43. with open('name/space/__init__.py', 'wt') as f:
  44. f.write('#empty\n')
  45. # name/space/tests/__init__.py
  46. with open('name/space/tests/__init__.py', 'wt') as f:
  47. f.write(TEST_PY)
  48. @pytest.fixture
  49. def quiet_log():
  50. # Running some of the other tests will automatically
  51. # change the log level to info, messing our output.
  52. log.set_verbosity(0)
  53. @pytest.mark.usefixtures('sample_test', 'quiet_log')
  54. @ack_2to3
  55. def test_test(capfd):
  56. params = dict(
  57. name='foo',
  58. packages=['name', 'name.space', 'name.space.tests'],
  59. namespace_packages=['name'],
  60. test_suite='name.space.tests.test_suite',
  61. use_2to3=True,
  62. )
  63. dist = Distribution(params)
  64. dist.script_name = 'setup.py'
  65. cmd = test(dist)
  66. cmd.ensure_finalized()
  67. cmd.run()
  68. out, err = capfd.readouterr()
  69. assert out == 'Foo\n'
  70. @pytest.mark.usefixtures('tmpdir_cwd', 'quiet_log')
  71. def test_tests_are_run_once(capfd):
  72. params = dict(
  73. name='foo',
  74. packages=['dummy'],
  75. )
  76. with open('setup.py', 'wt') as f:
  77. f.write('from setuptools import setup; setup(\n')
  78. for k, v in sorted(params.items()):
  79. f.write(' %s=%r,\n' % (k, v))
  80. f.write(')\n')
  81. os.makedirs('dummy')
  82. with open('dummy/__init__.py', 'wt'):
  83. pass
  84. with open('dummy/test_dummy.py', 'wt') as f:
  85. f.write(DALS(
  86. """
  87. import unittest
  88. class TestTest(unittest.TestCase):
  89. def test_test(self):
  90. print('Foo')
  91. """))
  92. dist = Distribution(params)
  93. dist.script_name = 'setup.py'
  94. cmd = test(dist)
  95. cmd.ensure_finalized()
  96. cmd.run()
  97. out, err = capfd.readouterr()
  98. assert out == 'Foo\n'
  99. @pytest.mark.usefixtures('sample_test')
  100. @ack_2to3
  101. def test_warns_deprecation(capfd):
  102. params = dict(
  103. name='foo',
  104. packages=['name', 'name.space', 'name.space.tests'],
  105. namespace_packages=['name'],
  106. test_suite='name.space.tests.test_suite',
  107. use_2to3=True
  108. )
  109. dist = Distribution(params)
  110. dist.script_name = 'setup.py'
  111. cmd = test(dist)
  112. cmd.ensure_finalized()
  113. cmd.announce = mock.Mock()
  114. cmd.run()
  115. capfd.readouterr()
  116. msg = (
  117. "WARNING: Testing via this command is deprecated and will be "
  118. "removed in a future version. Users looking for a generic test "
  119. "entry point independent of test runner are encouraged to use "
  120. "tox."
  121. )
  122. cmd.announce.assert_any_call(msg, log.WARN)
  123. @pytest.mark.usefixtures('sample_test')
  124. @ack_2to3
  125. def test_deprecation_stderr(capfd):
  126. params = dict(
  127. name='foo',
  128. packages=['name', 'name.space', 'name.space.tests'],
  129. namespace_packages=['name'],
  130. test_suite='name.space.tests.test_suite',
  131. use_2to3=True
  132. )
  133. dist = Distribution(params)
  134. dist.script_name = 'setup.py'
  135. cmd = test(dist)
  136. cmd.ensure_finalized()
  137. cmd.run()
  138. out, err = capfd.readouterr()
  139. msg = (
  140. "WARNING: Testing via this command is deprecated and will be "
  141. "removed in a future version. Users looking for a generic test "
  142. "entry point independent of test runner are encouraged to use "
  143. "tox.\n"
  144. )
  145. assert msg in err