pytest icon indicating copy to clipboard operation
pytest copied to clipboard

Allow test selection by file:lineno

Open blueyed opened this issue 8 years ago • 15 comments

pytest tests/test_foo.py:42 should run the (nearest) test at line 42 in tests/test_foo.py.

When it also ignores a trailing colon it would allow for easy copy'n'pasting from failures.

Via #5973:

To get a better overview of how it can work take a look at the Rspec documentation

blueyed avatar Feb 14 '18 21:02 blueyed

Great idea!

nicoddemus avatar Feb 17 '18 23:02 nicoddemus

How do we pick the nearest test? Assuming, we have a following file:

def test_1():
     assert True

def test_2():
     assert False

If the user will pick line 3, should it match test_1 or test_2? What if the line number falls in the fixture definition?

aklajnert avatar Sep 27 '19 05:09 aklajnert

picking whitespace or non test functions should result in a collection error and not running any tests

RonnyPfannschmidt avatar Sep 27 '19 05:09 RonnyPfannschmidt

s/whitespace/whitespace between test functions/

one must pick a line within the test function body

RonnyPfannschmidt avatar Sep 27 '19 05:09 RonnyPfannschmidt

In case you are interested: I am taking at stab at this in https://github.com/blueyed/pytest/pull/42, since I have "file:lnum" in summaries now (https://github.com/blueyed/pytest/pull/38).

blueyed avatar Nov 01 '19 10:11 blueyed

@blueyed Please don't close issues here for things you only fixed in your fork (instead of here)...

The-Compiler avatar Nov 01 '19 12:11 The-Compiler

@The-Compiler This is done automatically (Github bug/issue?) - maybe only for issues reported by myself? I will usually re-open them then if applicable (when going through my notifications).

blueyed avatar Nov 01 '19 13:11 blueyed

Just for reference, since I've found this issue again: I'm using this myself since a while, and it is very nice (as expected ;) - I know why I've asked about it several times, and am glad I've just tried it instead of adhering to the "this is blocked due to necessary refactorings"). /cc @feuillemorte

The next step would be using ranges here, e.g. fname:lnum- would run everything after that given line (useful from git diffs, where you want to run anything after a given / changed line).

blueyed avatar Nov 09 '19 19:11 blueyed

hey @blueyed thanks for this feature! I've just imagined this exactly like it is, and while I was writing an issue with the proposal, github showed me this one as a "related issue". What a synced community!

regarding the range support, would be great to also support an endline number instead to assume it's from lnum until the eof, so I could select a range in my editor and run "all the selected tests".

Moreover, it could mimic the python's slices behavior:

  • fname:fromline-endline,
  • fname:-endline (assumes "from the beginning)
  • fname:fromline- (assumes "until the end")

mgaitan avatar Nov 25 '19 13:11 mgaitan

As OP I'm closing it again: it's fixed for me (literally).

blueyed avatar Nov 27 '19 13:11 blueyed

What's the status of this?

I've found this code by @asfaltboy that basically solves the parsing problem using stdlib's ast.

mgaitan avatar Oct 13 '20 18:10 mgaitan

Hi @mgaitan,

Thanks for the link!

It is still open and AFAIK nobody is working on this currently.

nicoddemus avatar Oct 13 '20 18:10 nicoddemus

@blueyed @nicoddemus I see someone opened a PR for this (years ago). Any movement on review?

jasondamour avatar Oct 12 '24 22:10 jasondamour

They opened it against their own repo so we had no idea

RonnyPfannschmidt avatar Oct 13 '24 03:10 RonnyPfannschmidt

It's a great feature 👍

jinhucheung avatar Mar 13 '25 16:03 jinhucheung