Change:
const profileService = accessor.get(ITestProfileService); let bestNodes: InternalTestItem[] = []; <ins> let bestNodesBefore: InternalTestItem[] = []; </ins> let bestRange: IRange | undefined; <ins> let bestRangeBefore: IRange | undefined; </ins> // testsInFile will descend in the test tree. We assume that as we go // deeper, ranges get more specific. We'll want to run all tests whose // range is equal to the most specific range we find (see #133519) <ins> // // If we don't find any test whose range contains the position, we pick // the closest one before the position. Again, if we find several tests // whose range is equal to the closest one, we run them all. </ins> await showDiscoveringWhile(accessor.get(IProgressService), (async () => { for await (const test of testsInFile(testService.collection, model.uri)) { <del> if (!test.item.range || !Range.containsPosition(test.item.range, position) || !(profileService.capabilitiesForTest(test) & this.group)) { </del> <ins> if (!test.item.range || !(profileService.capabilitiesForTest(test) & this.group)) { </ins> continue; } <del> if (bestRange && Range.equalsRange(test.item.range, bestRange)) { bestNodes.push(test); } else { bestRange = test.item.range; bestNodes = [test]; </del> <ins> if (Range.containsPosition(test.item.range, position)) { if (bestRange && Range.equalsRange(test.item.range, bestRange)) { bestNodes.push(test); } else { bestRange = test.item.range; bestNodes = [test]; } } else if (test.item.range.startLineNumber < position.lineNumber || (test.item.range.startLineNumber === position.lineNumber && test.item.range.startColumn < position.column)) { if (bestRangeBefore) { if (Range.equalsRange(test.item.range, bestRangeBefore)) { bestNodesBefore.push(test); } else if (test.item.range.startLineNumber > bestRangeBefore.startLineNumber || (test.item.range.startLineNumber === bestRangeBefore.startLineNumber && test.item.range.startColumn > bestRangeBefore.startColumn)) { bestRangeBefore = test.item.range; bestNodesBefore = [test]; } } else { bestRangeBefore = test.item.range; bestNodesBefore = [test]; } </ins> } } })());

Discussion:
<!-- Do Not Delete This! bugreporttemplate --- VS Code Version: 1.62.3 OS Version: macOS 11.6.1 For C++ tests using the C++ TestMate extension, "Test: Run Test at Cursor" only works if the cursor is on the first line of the test case at column 0. For python tests, it works if the cursor is anywhere on the first line of the test case, up to column 0 of the next line. This is way too restrictive; if my cursor is on the first line of the test, I can just as well click the green icon that's right next to it. The point of the "Run Test at Cursor" is that it should work when my cursor is anywhere inside the test case (with the beginning of the test case potentially scrolled out of view). And that's how it worked in Test Explorer UI; I'm missing that behavior. Now, you might argue that it's the extension's responsibility to set the range of the TestItem appropriately to cover the whole test case. Yes, the C++ TestMate extension sets the range to , and the Python extension to , hence the behavior described above. However, I'd argue that this is too much of a burden for extension developers. Most underlying test frameworks only report the starting lines of test cases; actually I'm not aware of a single one that reports where a test case ends. This means that all extensions would have to implement the same heuristic to extend a TestItem's range until either the next item in the same file, or the end of the file if there's no next one. I'd argue that this should be done by the testing framework. Long story short: I'm proposing to treat the "no-man's land" between two TestItems (or after the last item) in a file as belonging to the item before it. Does this make sense, (Cc
Sure, we can use the closest test before the cursor if there's no exact range. I'm on vacation for a few weeks but PR's welcome. Code pointer:
Seconded
Here's a PR:
/verified Running test at cursor works no matter where the cursor is (and runs previous test in the no man's land)