testing.py 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # -*- coding: utf-8 -*-
  2. """Shared testing code."""
  3. from plone.testing import Layer
  4. from unittest2 import TestCase
  5. from os.path import dirname, join, abspath
  6. import os
  7. import subprocess
  8. class SearxTestLayer:
  9. """Base layer for non-robot tests."""
  10. __name__ = u'SearxTestLayer'
  11. def setUp(cls):
  12. pass
  13. setUp = classmethod(setUp)
  14. def tearDown(cls):
  15. pass
  16. tearDown = classmethod(tearDown)
  17. def testSetUp(cls):
  18. pass
  19. testSetUp = classmethod(testSetUp)
  20. def testTearDown(cls):
  21. pass
  22. testTearDown = classmethod(testTearDown)
  23. class SearxRobotLayer(Layer):
  24. """Searx Robot Test Layer"""
  25. def setUp(self):
  26. os.setpgrp() # create new process group, become its leader
  27. # get program paths
  28. webapp = os.path.join(
  29. os.path.abspath(os.path.dirname(os.path.realpath(__file__))),
  30. 'webapp.py'
  31. )
  32. exe = 'python'
  33. # set robot settings path
  34. os.environ['SEARX_SETTINGS_PATH'] = abspath(
  35. dirname(__file__) + '/settings_robot.yml')
  36. # run the server
  37. self.server = subprocess.Popen(
  38. [exe, webapp],
  39. stdout=subprocess.PIPE,
  40. stderr=subprocess.STDOUT
  41. )
  42. def tearDown(self):
  43. os.kill(self.server.pid, 9)
  44. # remove previously set environment variable
  45. del os.environ['SEARX_SETTINGS_PATH']
  46. SEARXROBOTLAYER = SearxRobotLayer()
  47. class SearxTestCase(TestCase):
  48. """Base test case for non-robot tests."""
  49. layer = SearxTestLayer
  50. if __name__ == '__main__':
  51. from tests.test_robot import test_suite
  52. import sys
  53. from zope.testrunner.runner import Runner
  54. base_dir = abspath(join(dirname(__file__), '../tests'))
  55. if sys.argv[1] == 'robot':
  56. r = Runner(['--color',
  57. '--auto-progress',
  58. '--stop-on-error',
  59. '--path',
  60. base_dir],
  61. found_suites=[test_suite()])
  62. r.run()
  63. sys.exit(int(r.failed))