runnables.scm

 1; subclasses of unittest.TestCase or TestCase
 2(
 3    (class_definition
 4        name: (identifier) @run @_unittest_class_name
 5        superclasses: (argument_list
 6            [(identifier) @_superclass
 7            (attribute (identifier) @_superclass)]
 8        )
 9        (#eq? @_superclass "TestCase")
10    ) @_python-unittest-class
11    (#set! tag python-unittest-class)
12)
13
14; test methods whose names start with `test` in a TestCase
15(
16    (class_definition
17        name: (identifier) @_unittest_class_name
18        superclasses: (argument_list
19            [(identifier) @_superclass
20            (attribute (identifier) @_superclass)]
21        )
22        (#eq? @_superclass "TestCase")
23        body: (block
24                (function_definition
25                    name: (identifier) @run @_unittest_method_name
26                    (#match? @_unittest_method_name "^test.*")
27                ) @_python-unittest-method
28                (#set! tag python-unittest-method)
29            )
30        )
31)
32
33; pytest functions
34(
35    (module
36        (function_definition
37            name: (identifier) @run @_pytest_method_name
38            (#match? @_pytest_method_name "^test_")
39        ) @python-pytest-method
40    )
41    (#set! tag python-pytest-method)
42)
43
44; pytest classes
45(
46    (module
47        (class_definition
48            name: (identifier) @run @_pytest_class_name
49            (#match? @_pytest_class_name "^Test")
50        )
51        (#set! tag python-pytest-class)
52    )
53)
54
55; pytest class methods
56(
57    (module
58        (class_definition
59            name: (identifier) @_pytest_class_name
60            (#match? @_pytest_class_name "^Test")
61            body: (block
62                    (function_definition
63                        name: (identifier) @run @_pytest_method_name
64                        (#match? @_pytest_method_name "^test")
65                    ) @python-pytest-method
66                    (#set! tag python-pytest-method)
67            )
68        )
69    )
70)