- Additional Naming Parameters
- How to do this
- Parameterized Tests
- Only running on a specific machine
- Capturing approvals from CI
By default an approval file will take the form of:
classname.methodname.approved.txt
There are a few cases where this is not sufficient:
- You need more than one approval in a test method
- You are using parameterized tests
- You need different approvals per computer/OS/platform
The way this is handled is through the naming factories. This will give you approval tests with the following:
classname.methodname.additionalInformation.approved.txt
There is a global namer factory that can be accessed with a try/catch. While this way works, it is not thread-safe and can cause trouble when running tests in parallel. Here is an example of how to do it:
try (NamedEnvironment namer = NamerFactory.withParameters("title", "chapter"))
{
Approvals.verify("data");
}This will result in a file named:
ClassName.methodName.firstString.secondString.approved.txt
With the addition of Options you can now set additional naming information in a thread-safe way. Here is an example of how to do that:
Approvals.verify("data", Approvals.NAMES.withParameters("title", "chapter"));This will result in a file named:
ClassName.methodName.firstString.secondString.approved.txt
If there is already an Options created, you can add to it using the .and keyword. Here is an example:
Options options = Approvals.NAMES.asOsSpecificTest().and(Approvals.NAMES::asMachineNameSpecificTest);
ApprovalNamer namer = options.forFile().getNamer();
String name = namer.getApprovedFile(".txt").getName();
assertEquals("NamerFactoryForOptionsTest.testMachineSpecificTest.Mac_OS_X.lars-mbp-14.approved.txt", name);This will result in a file named:
ClassName.methodName.OSName.machineName.approved.txt
If you are using parameterized tests you will need to use additional names. See How to use Approvals.verify in Parameterized Tests.
Sometimes you have tests that cannot run on all of your machines. See How to run tests only on specific machines
If you are using special names per OS/platform your CI machines might not have a corresponding dev box. To handle this see How to capture .received. files from CI