ok, what's the best approach if tests need postgre...
# general
f
ok, what's the best approach if tests need postgresql?
r
In general or with pants?
f
ah, sorry, with pants 🙂
in general I'd just start a docker container for it
e
Not sure if it’s the best approach, but I’m running
shell_command
which run docker compose or docker run as dependency of test target
f
ah ok, I'll look into it, thx
d
Alternatively you can use a library (this is not pants specific), e.g. https://pypi.org/project/pytest-docker/
e
@dry-architect-80370 are you successfully using that with Pants? The way Pants runs tests is not what most plugin code would expect and you either have to adjust using Pants config tweaks or you simply can't use the plugin as-is. In particular, session scope in pytest doesn't do what you might think it does under Pants runs.
d
Yes, it's been working for me so far but I haven't checked if the session scope behaviour is as expected (I have only a single test that uses that plugin).
e
Ah, ok. The moment you have >1 you may be in trouble. Pants runs each test file in its own process by default; so session scope only applies to that 1 run. If you have 200 test files using the session scoped fixture and 4 cores, you will get 4 concurrent session scopes running continuously over 50 rounds.
Sometimes you can use https://www.pantsbuild.org/docs/reference-pytest#execution_slot_var, sometimes you need to have some custom code that uses something like file locks to coordinate between processes, etc.
d
I see, thanks for the explanation, will keep that in mind!
b
Not sure if it’s the best approach, but I’m running
shell_command
which run docker compose or docker run as dependency of test target
As a warning, unfortunately this isn't a perfect approach:
shell_command
is designed for producing files on disk, and is cacheable. If the input dependencies are the same as a previous run, the command will not run again, and the output files will just be copied from Pants' cache. This means that if the docker containers stop running, Pants probably won't restart them because it won't rerun the command. For us, we just start the containers outside pants, and have tests fail if they're not already started (not great, but it's been acceptable so far). https://github.com/pantsbuild/pants/issues/16362 is a feature request covering this sort of thing.
f