Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set path for approved tests in code rather than config file #137

Closed
nlohmann opened this issue Mar 9, 2023 · 4 comments
Closed

Set path for approved tests in code rather than config file #137

nlohmann opened this issue Mar 9, 2023 · 4 comments

Comments

@nlohmann
Copy link

nlohmann commented Mar 9, 2023

I have a test suite with over 64k tests from a single function in pytest. All approved tests are written to the same directory, which gets a bit crowded. I would like to create subdirectories to distribute the files.

Is there a way to set the directory of the approved tests in code? So far, I only found approvaltests_config.json, but this sets it for the whole test suite which is not sufficient for me.

@nitsanavni
Copy link
Contributor

nitsanavni commented Mar 10, 2023

I think this can be acheived with specifying a namer, example:

verify(received, namer=CliNamer(test_id=test_id))

the verify call can be configured with:

verify(result, options=Options().with_namer(namer))

Maybe a good start is to extend NamerBase

@nlohmann
Copy link
Author

Thanks, I made it work!

@nitsanavni
Copy link
Contributor

Cool, can you share your solution?

@nlohmann
Copy link
Author

Sure:

# parameter is a string passed by pytest
# test_suite is a string passed by each test suite to create individual folders

# we want JSON output
options = approvaltests.Options({'extension_with_dot': '.json'})

# pass the parameter and set the output directory
namer = approvaltests.ScenarioNamer(approvaltests.get_default_namer(), parameter)
namer.base_namer.get_file_name()
namer.base_namer.config = {'subdirectory': os.path.join('approved_files', test_suite)}
namer.base_namer.config_loaded = True
options = options.with_namer(namer)

# json_object is built by the test and is a dict

# actual approval and passing our options
approvaltests.verify(data=json_object, encoding='utf-8', newline="\n", options=options)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants