1

Suppose that I am writing a test called test_this_thing in IPython which I intend to progressively save into a test file test_some_tests.py.

When it comes to running my tests I can do that easily from IPython with this great suggestion from a colleague:

%run -m pytest -- -k test_this_thing

Suppose I added a couple of fixtures into my test file (marked with @pytest.fixture). To get them into my IPython REPL I have to import them and call them (since they are written as functions):

from test_some_tests import *

fixture1 = fixture1()
fixture2 = fixture2()

I wonder if there is any kind of way to leverage pytest's fixture-finding functionality to write an IPython magic function like:

%import_fixtures -k test_this_thing

which would import into my IPython all the fixtures that the -k test_this_thing tests need and do the f = f() bit for me on each of them?

Note: When I write tests in IPython I will usually be running it from the directory where my test file lives (ie I cd into that directory before running ipython), so that

from test_some_tests import *

works. Some people might add various __init__.py to make their tests importable but I tend not to (indeed, IIRC the pytest suggestion is to not do that).

Robert
  • 1,530
  • 1
  • 16
  • 24
  • https://stackoverflow.com/a/48739098/2641825 suggests starting an ipython shell within the package directory and then running `!pytest --pdb` to enter inside the function that is causing a test to fail. Objects in that function can then be inspected at the [python debugger](https://docs.python.org/3/library/pdb.html) prompt. – Paul Rougieux Jun 06 '23 at 08:28

1 Answers1

1

I came up with an alternative which works and which brings one advantage -- it imports my underscore helper functions too.

At the bottom of the test file I added:

if __name__ == "__main__":
    fixture1 = fixture1()
    fixture2 = fixture2()

and then in IPython, rather than

from test_some_tests import *

I do

%run test_some_tests.py

The advantage is that this also imports into IPython all my underscore helper functions.

Robert
  • 1,530
  • 1
  • 16
  • 24