testing.py 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # -*- coding: utf-8 -*-
  2. """Shared testing code."""
  3. from plone.testing import Layer
  4. from unittest2 import TestCase
  5. import os
  6. import subprocess
  7. class SearxTestLayer:
  8. """Base layer for non-robot tests."""
  9. __name__ = u'SearxTestLayer'
  10. def setUp(cls):
  11. pass
  12. setUp = classmethod(setUp)
  13. def tearDown(cls):
  14. pass
  15. tearDown = classmethod(tearDown)
  16. def testSetUp(cls):
  17. pass
  18. testSetUp = classmethod(testSetUp)
  19. def testTearDown(cls):
  20. pass
  21. testTearDown = classmethod(testTearDown)
  22. class SearxRobotLayer(Layer):
  23. """Searx Robot Test Layer"""
  24. def setUp(self):
  25. os.setpgrp() # create new process group, become its leader
  26. # get program paths
  27. webapp = os.path.join(
  28. os.path.abspath(os.path.dirname(os.path.realpath(__file__))),
  29. 'webapp.py'
  30. )
  31. exe = os.path.abspath(os.path.dirname(__file__) + '/../bin/py')
  32. # set robot settings path
  33. os.environ['SEARX_SETTINGS_PATH'] = os.path.abspath(
  34. os.path.dirname(__file__) + '/settings_robot.yml')
  35. # run the server
  36. self.server = subprocess.Popen(
  37. [exe, webapp],
  38. stdout=subprocess.PIPE,
  39. stderr=subprocess.STDOUT
  40. )
  41. def tearDown(self):
  42. os.kill(self.server.pid, 15)
  43. # remove previously set environment variable
  44. del os.environ['SEARX_SETTINGS_PATH']
  45. SEARXROBOTLAYER = SearxRobotLayer()
  46. class SearxTestCase(TestCase):
  47. """Base test case for non-robot tests."""
  48. layer = SearxTestLayer