0

I saw this question, asking the same about doing things before tests.

I need to do things before fixtures.

For example, I am setting up a dockerized environment, which I have to clean before building. To make things more complicated, I am using this plugin which defines fixtures I can't control or change, and I need something that comes before all fixtures (and does docker-compose down and other cleanup).

For example, when pytest starts, run the common per-session pre-step, then the fixtures, then the per-module pre-step, then the fixtures, and so on.

Is this a supported hook in pytest?

I couldn't find a relevant doc.

Gulzar
  • 23,452
  • 27
  • 113
  • 201
  • Have you checked the `sessionstart` / `sessionfinish` hooks? – MrBean Bremen Jan 31 '22 at 10:32
  • @MrBeanBremen It works at the start of the session. Is there a clean way to use simple fixtures within that function? For example, `def my_name(): return "Gulzar"` can be required for initialization, and also as a fixture. I can extract a function, but that seems like overkill. Does something better exist? – Gulzar Feb 08 '22 at 14:48

1 Answers1

0

As @MrBean Bremen stated, pytest_sessionstart does the trick.

An annoying problem with that, is I can't use fixtures inside (naturally), with Argument(s) {'fixture_name'} are declared in the hookimpl but can not be found in the hookspec

I tried to fix that with pytest-dependency, but it doesn't seem to work on fixtures, only on tests.

I am left with the hacky workaround to extract required data for sessionstart to functions, and call them in sessionstart as well as in similarly named fixtures.

It works, but it is ugly.

Would accept a cleaner solution over this one

Gulzar
  • 23,452
  • 27
  • 113
  • 201