We are making some API changes that require us to ...
# general
g
We are making some API changes that require us to regenerate our testing snapshots (we're using pytest-snapshot, another more familiar tool is snapshottest). Before pants the way that we did this is to pass the
--update-snapshot
option to the pytest CLI. Now that we're on pants I'm unsure which mechanisms, targets, goals we would use instead. I can run
./pants test package/tests/test_example.py -- --update-snapshot
but the new snapshots I want to save are inside the sandbox. So how would I use my
python_tests
targets to generate new
resources
targets (which are ultimately Python files which contain the snapshots)?
h
There’s a pytest args option for the python tests subsystem
Check out args in there
g
We're able to pass in args to pytest without a problem. In our case this is a command line arg that we want to pass only when we're regenerating our screenshot files and not every time we run the test goal. When we do pass the command line arg it works as expected but inside the sandbox, the new files are inside the sandbox which isn't helpful because we want to persist our new snapshots as
resource
targets for the tests to use.
h
Ah. Sounds like you’re approaching plug-in territory to ship the generated files to dist/ where other exports go
You could probably use adhoc tool or something like that to capture the outputs
h
Yeah, there is a ticket for this feature - https://github.com/pantsbuild/pants/issues/11622
And thanks to @witty-family-13337’s stellar work on snapshot capture for the helm backend, a lot of the plumbing is now in place
it just needs to be implemented for pytest
This could be done by a motivated user (hint hint) by following the example of the helm backend
w
thanks a lot @happy-kitchen-89482 , it was quite easy to get it working for Helm. Unfortunately I’m not that familiar with this feature in pytest and how users usually interact with it but happy to give a hand if someone wants to have a go at it
g
Yeah, I will take a look at Alonso's code on the helm snapshots. I'm not using it on a pants repo, but there's another similar use-case on the python side for test cassettes as well. Basically I'd like to be able to find new/changed artifacts that are created alongside the
python_test
targets and move them out of the sandbox and into the repo so they can become
resource
targets. @witty-family-13337 it sounds like the helm snapshots are doing something similar? Depending on the testing plugin / config you're using these files could show up in folders with different naming conventions and could also be YAML, Python, or other types of files. I'm not familiar with helm very much but it sounds like snapshots are a supported tool by helm itself and not implemented as a third party plugin. This seems more convenient as the file naming pattern / format would be consistent.
w
yeah, you’re more or less spot on. The Helm snapshots are a feature of a Helm plugin named
unittest
. In this plugin’s context, snapshots are YAML files that live in a folder with named
__snapshot__
(no option to change it). The implementation in Pants rests in 3 pilars: 1. Tailor: Being capable of detecting the existence of snapshot folders and generate
resource
targets for them. 2. Dependency Inference: On the presence of
resources
targets in a snapshot folder (relative to the test sources), then establish a dependency relationship between the test sources and the snapshot resources (the user should still be capable to explicitly negate that dependency like in other cases supported by Pants) 3. Generate Snapshots: This is a new goal that would trigger the generation of new or updates test snapshots. If new snapshots are generated, then a new tailor run may be needed to get the
resources
targets generated again.
I think in Pytest the name of the folder is not so much written in stone like in Helm Unittest, which could be an option through the pytest subsystem.
but as a first go it may be easier to assume a hardcoded folder name and keep improving it from there
g
Great, super helpful. Thank you!
🙌 1
h
You can start with just #3, and require manual addition of resources targets and the deps on them, as a first step.
That's the meat of it
Then #1 and #2 can be follow-ons