collection of python libs developed for testing purposes

example_test.py 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/python
  2. import httplib
  3. import operator
  4. import subprocess
  5. import unittest
  6. import urlparse
  7. from sznqalibs import hoover
  8. class BaseCalcDriver(hoover.BaseTestDriver):
  9. def __init__(self):
  10. super(BaseCalcDriver, self).__init__()
  11. self._mandatory_args = ['op', 'a', 'b']
  12. def bailout_on_zerodiv(argset):
  13. if argset['op'] == 'div':
  14. return argset['b'] == 0
  15. return False
  16. self.bailouts = [bailout_on_zerodiv]
  17. class PyCalcDriver(BaseCalcDriver):
  18. def _get_data(self):
  19. ops = {
  20. 'add': lambda a, b: float(a) + b,
  21. 'sub': lambda a, b: float(a) - b,
  22. 'mul': lambda a, b: float(a) * b,
  23. 'div': lambda a, b: float(a) / b,
  24. }
  25. a = self._args['a']
  26. b = self._args['b']
  27. op = self._args['op']
  28. self.data['result'] = ops[op](a, b)
  29. class CgiCalcDriver(BaseCalcDriver):
  30. def __init__(self):
  31. super(CgiCalcDriver, self).__init__()
  32. self._mandatory_settings = ['uri']
  33. def _get_data(self):
  34. pq = "op=%(op)s&a=%(a)s&b=%(b)s" % self._args
  35. parsed_url = urlparse.urlparse(self._settings['uri'])
  36. conn = httplib.HTTPConnection(parsed_url.hostname)
  37. conn.request("GET", "%s?%s" % (parsed_url.path, pq))
  38. resp = conn.getresponse()
  39. assert resp.status == 200
  40. self.data['result'] = float(resp.read().strip())
  41. class CliCalcDriver(BaseCalcDriver):
  42. def __init__(self):
  43. super(CliCalcDriver, self).__init__()
  44. self._mandatory_settings = ['cmd']
  45. def _get_data(self):
  46. cmd = [
  47. self._settings['cmd'],
  48. self._args['op'],
  49. str(self._args['a']),
  50. str(self._args['b']),
  51. ]
  52. out = subprocess.check_output(cmd)
  53. self.data['result'] = float(out.strip())
  54. class TestCase(unittest.TestCase):
  55. def setUp(self):
  56. self.driver_settings = {
  57. 'uri': "http://myserver/cgi-bin/calc.cgi",
  58. 'cmd': "./calc.sh"
  59. }
  60. self.scheme = dict.fromkeys(['op', 'a', 'b'], hoover.Cartman.Iterable)
  61. def test_using_rt(self):
  62. argsrc = hoover.Cartman({
  63. 'op': ['add', 'sub', 'mul', 'div'],
  64. 'a': [-10, -1, 1, 10, 1000],
  65. 'b': [-10, -1, 1, 10, 1000],
  66. }, self.scheme
  67. )
  68. tests = [
  69. (operator.eq, PyCalcDriver, CliCalcDriver),
  70. (operator.eq, PyCalcDriver, CliCalcDriver),
  71. ]
  72. tracker = hoover.regression_test(argsrc, tests, self.driver_settings)
  73. print hoover.jsDump(tracker.getstats())
  74. if tracker.errors_found():
  75. self.fail(tracker.format_report())
  76. if __name__ == '__main__':
  77. unittest.main()