[python] PY-72251 Missing test run gutter icon for nested test classes

Merge-request: IJ-MR-181131
Merged-by: Egor Eliseev <Egor.Eliseev@jetbrains.com>

GitOrigin-RevId: bae956b8083159c8f28391d058306afef60bf349
This commit is contained in:
chbndrhnns
2025-12-06 15:30:13 +00:00
committed by intellij-monorepo-bot
parent 2386948c60
commit be0a2a737e
3 changed files with 29 additions and 9 deletions
@@ -54,21 +54,24 @@ fun isTestClass(clazz: PyClass, context: TypeEvalContext): Boolean {
return false
}
object : Processor<PyFunction> {
var hasTestFunction: Boolean = false
private set
// Check if any method is a test method or setUp
var hasTestMethod = false
clazz.visitMethods(object : Processor<PyFunction> {
override fun process(function: PyFunction): Boolean {
if (isTestFunction(function) || function.name?.equals("setUp") == true) {
hasTestFunction = true
hasTestMethod = true
return false
}
return true
}
}.apply {
clazz.visitMethods(this, true, context)
return hasTestFunction
}
}, true, context)
// Check nested classes
val hasTestInNested = clazz.statementList.statements?.any { stmt ->
stmt is PyClass && isTestClass(stmt, context)
} ?: false
return hasTestMethod || hasTestInNested
}
fun isTestFile(
@@ -0,0 +1,8 @@
# Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
import pytest
class TestOuter<caret>most:
class TestAlsoOuter:
class TestInner:
def test_(self):
pass
@@ -12,6 +12,7 @@ open class PyTestRunLineMarkerTest : PyTestCase() {
const val TESTS_DIR = "/pyTestLineMarker/"
const val PYTHON_FILE = "pythonFile.py"
const val FIXTURE_FILE = "fixtureTest.py"
const val PYTHON_FILE_WITH_NESTED_CLASSES = "nestedTest.py"
}
override fun getTestDataPath(): String = super.getTestDataPath() + TESTS_DIR
@@ -73,4 +74,12 @@ open class PyTestRunLineMarkerTest : PyTestCase() {
assertNotNull(fixtureElement)
assertInfoNotFound(fixtureElement!!, lineMarkerContributor)
}
fun testNestedTestClass() {
val lineMarkerContributor = PyTestLineMarkerContributor()
val element = getCaretElement(PYTHON_FILE_WITH_NESTED_CLASSES)
if (element != null) {
assertInfoFound(element, lineMarkerContributor)
}
}
}