yaml_helpers.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. """
  2. """
  3. import pytest
  4. import yaml
  5. def generate_yaml_tests(directory):
  6. for yml_file in directory.glob("*/*.yml"):
  7. data = yaml.safe_load(yml_file.read_text())
  8. assert "cases" in data, "A fixture needs cases to be used in testing"
  9. # Strip the parts of the directory to only get a name without
  10. # extension and resolver directory
  11. base_name = str(yml_file)[len(str(directory)) + 1:-4]
  12. base = data.get("base", {})
  13. cases = data["cases"]
  14. for i, case_template in enumerate(cases):
  15. case = base.copy()
  16. case.update(case_template)
  17. case[":name:"] = base_name
  18. if len(cases) > 1:
  19. case[":name:"] += "-" + str(i)
  20. if case.pop("skip", False):
  21. case = pytest.param(case, marks=pytest.mark.xfail)
  22. yield case
  23. def id_func(param):
  24. """Give a nice parameter name to the generated function parameters
  25. """
  26. if isinstance(param, dict) and ":name:" in param:
  27. return param[":name:"]
  28. retval = str(param)
  29. if len(retval) > 25:
  30. retval = retval[:20] + "..." + retval[-2:]
  31. return retval