MilesCranmer commited on
Commit
b271a60
1 Parent(s): 5dfd8d5

Add ability to filter tests

Browse files
Files changed (1) hide show
  1. pysr/_cli/main.py +17 -4
pysr/_cli/main.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import sys
2
  import unittest
3
  import warnings
@@ -52,7 +53,14 @@ TEST_OPTIONS = {"main", "jax", "torch", "cli", "dev", "startup"}
52
 
53
  @pysr.command("test")
54
  @click.argument("tests", nargs=1)
55
- def _tests(tests):
 
 
 
 
 
 
 
56
  """Run parts of the PySR test suite.
57
 
58
  Choose from main, jax, torch, cli, dev, and startup. You can give multiple tests, separated by commas.
@@ -78,11 +86,16 @@ def _tests(tests):
78
  loader = unittest.TestLoader()
79
  suite = unittest.TestSuite()
80
  for test_case in test_cases:
81
- suite.addTests(loader.loadTestsFromTestCase(test_case))
 
 
 
 
 
 
 
82
  runner = unittest.TextTestRunner()
83
  results = runner.run(suite)
84
- # Normally unittest would run this, but here we have
85
- # to do it manually to get the exit code.
86
 
87
  if not results.wasSuccessful():
88
  sys.exit(1)
 
1
+ import fnmatch
2
  import sys
3
  import unittest
4
  import warnings
 
53
 
54
  @pysr.command("test")
55
  @click.argument("tests", nargs=1)
56
+ @click.option(
57
+ "-k",
58
+ "expressions",
59
+ multiple=True,
60
+ type=str,
61
+ help="Filter expressions to select specific tests.",
62
+ )
63
+ def _tests(tests, expressions):
64
  """Run parts of the PySR test suite.
65
 
66
  Choose from main, jax, torch, cli, dev, and startup. You can give multiple tests, separated by commas.
 
86
  loader = unittest.TestLoader()
87
  suite = unittest.TestSuite()
88
  for test_case in test_cases:
89
+ loaded_tests = loader.loadTestsFromTestCase(test_case)
90
+ for test in loaded_tests:
91
+ if len(expressions) == 0 or any(
92
+ fnmatch.fnmatch(test.id(), "*" + expression + "*")
93
+ for expression in expressions
94
+ ):
95
+ suite.addTest(test)
96
+
97
  runner = unittest.TextTestRunner()
98
  results = runner.run(suite)
 
 
99
 
100
  if not results.wasSuccessful():
101
  sys.exit(1)