Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove gutter icon for pytest fixtures #2970

Open
wants to merge 1 commit into
base: 251
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,26 @@ import com.jetbrains.python.psi.PyFunction
import com.jetbrains.python.psi.types.TypeEvalContext

/**
* Any "test_" function could be used as test by pytest.
* Any "test_" function could be used as a test by pytest.
* See `PythonUnitTestDetectorsBasedOnSettings.isTestFunction`.
*/
fun isTestFunction(function: PyFunction) = function.name?.startsWith("test") == true
fun isTestFunction(function: PyFunction): Boolean {
// Check if the function name starts with "test"
val isTestNamed = function.name?.startsWith("test") == true
if (!isTestNamed) return false

// Get the list of decorators, if any, otherwise consider this a test function
val decoratorList = function.decoratorList?.decorators ?: return true

// Determine if any decorator matches known pytest fixture names
val isPytestFixture = decoratorList.any { decorator ->
val decoratorName = decorator.callee?.text
decoratorName in com.jetbrains.python.testing.pyTestFixtures.TEST_FIXTURE_DECORATOR_NAMES
}

// A test function must not be a pytest fixture
return !isPytestFixture
}

/**
* Inheritor of TestCase class is always test for unittest and could also be launched with pytest.
Expand Down
8 changes: 8 additions & 0 deletions python/testData/pyTestLineMarker/fixtureTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pytest

@pytest.fixture
def test_fixture():
return 42

def test_actual():
assert True
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ open class PyTestRunLineMarkerTest : PyTestCase() {
companion object {
const val TESTS_DIR = "/pyTestLineMarker/"
const val PYTHON_FILE = "pythonFile.py"
const val FIXTURE_FILE = "fixtureTest.py"
}

override fun getTestDataPath(): String = super.getTestDataPath() + TESTS_DIR
Expand Down Expand Up @@ -54,4 +55,23 @@ open class PyTestRunLineMarkerTest : PyTestCase() {
assertInfoFound(element, lineMarkerContributor)
}
}
}

fun testFixtureWithTestPrefix() {
val lineMarkerContributor = PyTestLineMarkerContributor()

myFixture.configureByText(FIXTURE_FILE, """
import pytest

@pytest.fixture
def <caret>test_fixture():
return 42

def test_actual():
assert True
""".trimIndent())

val fixtureElement = myFixture.file.findElementAt(myFixture.caretOffset)
assertNotNull(fixtureElement)
assertInfoNotFound(fixtureElement!!, lineMarkerContributor)
}
}