testRandomOrder.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env python3
  2. """
  3. This test script verifies that the random ordering of tests inside
  4. Catch2 is invariant in regards to subsetting. This is done by running
  5. the binary 3 times, once with all tests selected, and twice with smaller
  6. subsets of tests selected, and verifying that the selected tests are in
  7. the same relative order.
  8. """
  9. import subprocess
  10. import sys
  11. import random
  12. def list_tests(self_test_exe, tags, rng_seed):
  13. cmd = [self_test_exe, '--list-test-names-only', '--order', 'rand',
  14. '--rng-seed', str(rng_seed)]
  15. tags_arg = ','.join('[{}]~[.]'.format(t) for t in tags)
  16. if tags_arg:
  17. cmd.append(tags_arg)
  18. process = subprocess.Popen(
  19. cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  20. stdout, stderr = process.communicate()
  21. if stderr:
  22. raise RuntimeError("Unexpected error output:\n" + process.stderr)
  23. result = stdout.split(b'\n')
  24. result = [s for s in result if s]
  25. if len(result) < 2:
  26. raise RuntimeError("Unexpectedly few tests listed (got {})".format(
  27. len(result)))
  28. return result
  29. def check_is_sublist_of(shorter, longer):
  30. assert len(shorter) < len(longer)
  31. assert len(set(longer)) == len(longer)
  32. indexes_in_longer = {s: i for i, s in enumerate(longer)}
  33. for s1, s2 in zip(shorter, shorter[1:]):
  34. assert indexes_in_longer[s1] < indexes_in_longer[s2], (
  35. '{} comes before {} in longer list.\n'
  36. 'Longer: {}\nShorter: {}'.format(s2, s1, longer, shorter))
  37. def main():
  38. self_test_exe, = sys.argv[1:]
  39. # We want a random seed for the test, but want to avoid 0,
  40. # because it has special meaning
  41. seed = random.randint(1, 2 ** 32 - 1)
  42. list_one_tag = list_tests(self_test_exe, ['generators'], seed)
  43. list_two_tags = list_tests(self_test_exe, ['generators', 'matchers'], seed)
  44. list_all = list_tests(self_test_exe, [], seed)
  45. # First, verify that restricting to a subset yields the same order
  46. check_is_sublist_of(list_two_tags, list_all)
  47. check_is_sublist_of(list_one_tag, list_two_tags)
  48. if __name__ == '__main__':
  49. sys.exit(main())