2

I'm using GNU Emacs 24.5.1 to work on Python code. I often want to run just a single unit test. I can do this, for example, by running:

test=spi.test_views.IndexViewTest.generate_select2_data_with_embedded_spaces  make test

with M-X compile. My life would be simpler if I could give some command like "Run the test where point is", and have emacs figure out the full name of the test for me. Is possible?

Update: with the folowing buffer, I'd like some command which runs M-X compile with:

test=spi.test_views.IndexViewTest.test_unknown_button make test

where spi is the name of the directory test_views.py is in. Well, technically, I need to construct the python path to my test function, but in practice, it'll be <directory.file.class.function>.

This seems like the kind of thing somebody would have already invented, but I don't see anything in the python mode docs.

enter image description here

3
  • 1
    Everything is possible, you just need to code it up. Commented Feb 8, 2021 at 19:11
  • Okay, let's give this another try: could you please edit your question to show the buffer you're in when you want to invoke the test? Commented Feb 9, 2021 at 20:50
  • 2
    While not really off-topic, you might get a better answer at emacs.stackexchange.com. Commented Feb 9, 2021 at 23:25

1 Answer 1

3

I believe you use the "default" python mode, while the so-called elpy mode (that I strongly recommend giving a try when doing Python developments within Emacs) seems to provide what you are looking for:

 C-c C-t (elpy-test)

Start a test run. This uses the currently configured test runner to discover
and run tests. If point is inside a test case, the test runner will run exactly
that test case. Otherwise, or if a prefix argument is given, it will run all tests.

Extra details

The elpy-test function internally relies on the function (elpy-test-at-point), which appears to be very close to the feature you mentioned in the question.

See e.g. the code/help excerpt in the following screenshot:

elpy-test

Sign up to request clarification or add additional context in comments.

1 Comment

Cool, elpy is exactly what I was looking for. Not stated in my original post was that these are django tests, so they need to run with the django test runner which elpy actually knows how to do (M-X elpy-set-test-runner)! Very neat, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.