runnables.scm

  1; subclasses of unittest.TestCase or TestCase
  2((class_definition
  3  name: (identifier) @run @_unittest_class_name
  4  superclasses: (argument_list
  5    [
  6      (identifier) @_superclass
  7      (attribute
  8        (identifier) @_superclass)
  9    ])
 10  (#eq? @_superclass "TestCase")) @_python-unittest-class
 11  (#set! tag python-unittest-class))
 12
 13; test methods whose names start with `test` in a TestCase
 14(class_definition
 15  name: (identifier) @_unittest_class_name
 16  superclasses: (argument_list
 17    [
 18      (identifier) @_superclass
 19      (attribute
 20        (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.*")) @_python-unittest-method
 27    (#set! tag python-unittest-method)))
 28
 29; pytest functions
 30((module
 31  (function_definition
 32    name: (identifier) @run @_pytest_method_name
 33    (#match? @_pytest_method_name "^test_")) @_python-pytest-method)
 34  (#set! tag python-pytest-method))
 35
 36; decorated pytest functions
 37((module
 38  (decorated_definition
 39    (decorator)+ @_decorator
 40    definition: (function_definition
 41      name: (identifier) @run @_pytest_method_name
 42      (#match? @_pytest_method_name "^test_"))) @_python-pytest-method)
 43  (#set! tag python-pytest-method))
 44
 45; pytest classes
 46(module
 47  (class_definition
 48    name: (identifier) @run @_pytest_class_name
 49    (#match? @_pytest_class_name "^Test"))
 50  (#set! tag python-pytest-class))
 51
 52; decorated pytest classes
 53(module
 54  (decorated_definition
 55    (decorator)+ @_decorator
 56    definition: (class_definition
 57      name: (identifier) @run @_pytest_class_name
 58      (#match? @_pytest_class_name "^Test")))
 59  (#set! tag python-pytest-class))
 60
 61; pytest class methods
 62(module
 63  (class_definition
 64    name: (identifier) @_pytest_class_name
 65    (#match? @_pytest_class_name "^Test")
 66    body: (block
 67      [
 68        (decorated_definition
 69          (decorator)+ @_decorator
 70          definition: (function_definition
 71            name: (identifier) @run @_pytest_method_name
 72            (#match? @_pytest_method_name "^test_")))
 73        (function_definition
 74          name: (identifier) @run @_pytest_method_name
 75          (#match? @_pytest_method_name "^test"))
 76      ] @_python-pytest-method)
 77    (#set! tag python-pytest-method)))
 78
 79; decorated pytest class methods
 80(module
 81  (decorated_definition
 82    (decorator)+ @_decorator
 83    definition: (class_definition
 84      name: (identifier) @_pytest_class_name
 85      (#match? @_pytest_class_name "^Test")
 86      body: (block
 87        [
 88          (decorated_definition
 89            (decorator)+ @_decorator
 90            definition: (function_definition
 91              name: (identifier) @run @_pytest_method_name
 92              (#match? @_pytest_method_name "^test_")))
 93          (function_definition
 94            name: (identifier) @run @_pytest_method_name
 95            (#match? @_pytest_method_name "^test"))
 96        ] @_python-pytest-method)
 97      (#set! tag python-pytest-method))))
 98
 99; module main method
100(module
101  (if_statement
102    condition: (comparison_operator
103      (identifier) @run @_lhs
104      operators: "=="
105      (string) @_rhs)
106    (#eq? @_lhs "__name__")
107    (#match? @_rhs "^[\"']__main__[\"']$")
108    (#set! tag python-module-main-method)))