utils.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. """
  2. Test helper functions
  3. """
  4. def probe_values_in_tuple(content, tuple_key, required_values):
  5. """
  6. Tests a tuple for required values, extracting the tuple beforehand.
  7. :param content: content string containing the tuple attribute (e.g. Django settings)
  8. :param tuple_key: attribute name of the tuple
  9. :param required_values: list or tuple of values for testing the tuple
  10. :return: None (asserts in case of failure)
  11. """
  12. try:
  13. start_pos = content.find("%s = (\n" % tuple_key)
  14. assert start_pos != -1, "Tuple not found: %s" % tuple_key
  15. stop_pos = 1 + content.find("\n)\n", start_pos)
  16. assert stop_pos > start_pos, "End of tuple not found: %s" % tuple_key
  17. tuple = content[start_pos:stop_pos]
  18. for val in required_values:
  19. val_line = (" '%s',\n" % val)
  20. assert val_line in tuple, "Not found in tuple %s: %s" % (tuple_key, val)
  21. return True
  22. except AssertionError as ae:
  23. print(ae.message)
  24. return False
  25. def pytest_generate_tests(metafunc):
  26. """
  27. A test scenarios implementation for py.test, as found at
  28. http://pytest.org/latest/example/parametrize.html#a-quick-port-of-testscenarios
  29. Picks up a ``scenarios`` class variable to parametrize all test function calls.
  30. """
  31. idlist = []
  32. argvalues = []
  33. for scenario in metafunc.cls.scenarios:
  34. idlist.append(scenario[0])
  35. items = scenario[1].items()
  36. argnames = [x[0] for x in items]
  37. argvalues.append(([x[1] for x in items]))
  38. metafunc.parametrize(argnames, argvalues, ids=idlist, scope="class")