Skip to content

Commit

Permalink
fix: handle nested functions in pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
martini97 committed Jun 19, 2023
1 parent 2ce70ed commit fd8b349
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
10 changes: 10 additions & 0 deletions test/assets/test_python_pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ def test_inside_namespace(self):
for i in range(5):
assert i < 5
assert True

def test_nested_function(self):
def foo():
return 5

assert foo() == 5

@pytest.mark.django_db
def test_decorated(self):
assert 1 == 1.0
8 changes: 7 additions & 1 deletion test/testrun-pytest-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ If it's outside of a class it should return the path to the file."
"test/assets/test_python_pytest.py::TestWithNamespace"))
(goto-char 205)
(should (equal (testrun-pytest-get-test "nearest")
"test/assets/test_python_pytest.py::TestWithNamespace::test_inside_namespace")))))
"test/assets/test_python_pytest.py::TestWithNamespace::test_inside_namespace"))
(goto-char 305)
(should (equal (testrun-pytest-get-test "nearest")
"test/assets/test_python_pytest.py::TestWithNamespace::test_nested_function"))
(goto-char 407)
(should (equal (testrun-pytest-get-test "nearest")
"test/assets/test_python_pytest.py::TestWithNamespace::test_decorated")))))

(ert-deftest test-testrun-pytest-get-test-namespace ()
"Verify expected test paths with the namespace scope.
Expand Down
9 changes: 7 additions & 2 deletions testrun-pytest.el
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

;;; Code:

(require 'rx)
(require 'seq)
(require 'testrun-core)
(require 'testrun-treesit)

Expand All @@ -45,10 +47,13 @@
"function_definition")
"List of node types relevant for the \"nearest\" scope.")

(defvar testrun-pytest-node-regex (rx bol (or "test" "Test") (1+ (or word "_")) eol))

(defun testrun-pytest--get-nodes (node-types)
"Return name of nodes with NODE-TYPES."
(mapcar #'testrun-treesit--get-node-name
(testrun-treesit--get-nodes-by-type node-types)))
(seq-filter (lambda (n) (string-match-p testrun-pytest-node-regex n))
(mapcar #'testrun-treesit--get-node-name
(testrun-treesit--get-nodes-by-type node-types))))

;;;###autoload
(defun testrun-pytest-get-test (scope)
Expand Down

0 comments on commit fd8b349

Please sign in to comment.