Custom Format Tokens - microsoft/UnitTestBoilerplateGenerator GitHub Wiki
Tokens can be inserted into a format field in the options page as $TokenName$
.
Global tokens are valid in all templates, including test file name and all content templates.
Token | Description | Example |
---|---|---|
Namespace | The namespace for the test class. Generated automatically from the default namespace for the test project, plus the subdirectory the test is generated under. | MyTests.Services |
ClassName | The name of the class being tested. | LoggerService |
ClassNameShort | An abbreviated version of the class name, used as a shorthand reference to the class. This is in PascalCase. |
For a tested class LoggerService -> Service |
Main content tokens are valid for the main template in the "Test File Contents" section.
Token | Description | Example |
---|---|---|
UsingStatements | All the using statements required for the file. Will include the test framework, mock framework and all referenced types from the tested class dependencies and the tested class itself. | using NSubstitute; using MyLibrary.Services; using System.Collections.Generic; using Xunit; |
MockFieldDeclarations | Declarations for the mock object fields. | private IStorage<string> subStorageString; private ISomeInterface subSomeInterface; |
MockFieldInitializations | Initializations for the mock object fields. | this.subStorageString = Substitute.For<IStorage<string>>(); this.subSomeInterface = Substitute.For<ISomeInterface>(); |
TestMethods | The test methods generated for each public method of the tested class | [TestMethod] public void AddNumbers_Test() { ... } |
ExplicitConstructor | The constructor for the tested class, with all dependencies from mock fields passed in. Also usable in the Test Method Invoke/Empty templates. | new LoggerService( this.subSomeInterface, this.subSomeOtherInterface) |
TodoConstructor | The constructor for the tested class, with "TODO" for all the arguments. This is used when there is no mock framework present. Also usable in the Test Method Invoke/Empty templates. | new LoggerService(TODO, TODO) |
Interface tokens are valid for the mock field declaration format, mock field initialization format and mock object reference format. They refer to a mocked dependency that's being passed in to the tested class. For each example in the following table, assume the type is IStorage<string>
.
Token | Description | Example |
---|---|---|
InterfaceName | The root interface name, minus generics | IStorage |
InterfaceNameBase | The root interface name, minus generics, with the initial "I" removed | Storage |
InterfaceType | The type of the interface, including generics. Is not fully qualified as the appropriate using statements should be added automatically. | IStorage<string> |
InterfaceMockName | A unique name for the mock object (Pascal case) | StorageString |
Test method tokens are valid for the test method invoke format. These refer to a specific public method from the source class being targeted for a test.
Token | Description | Example |
---|---|---|
TestedMethodName | The name of the tested method. This is also the only token valid within the "Test method name format" section | AddNumbers |
TestMethodName | The generated name for the test method. | AddNumbers_Test |
AsyncModifier | The "async" keyword for the test method, if needed. |
If the tested method is async -> async If it is not -> "" |
AsyncReturnType | The return type for the test method. |
If the tested method is async -> Task If it is not -> void |
ParameterSetupTodo | Sets up variables to hold parameter values. Uses "TODO" for the value, to force you to populate them before running the test. | int firstNumber = TODO; int secondNumber = TODO; |
ParameterSetupDefaults | Sets up variables to hold parameter values. Uses default value for that data type, so the test compiles right away. | int firstNumber = 0; int secondNumber = 0; |
MethodInvocationPrefix | Inserted before the method invocation. Takes into account if the method is async and if it returns a value |
If the method returns a value and is async -> var result = await If the method is async but does not return a value -> await If the method is not async and does not return a value -> "" |
MethodInvocation | Invokes the tested method. Assumes |
.AddNumbers(firstNumber, secondNumber) |
To use a token modifier, add a period '.' and the token modifier at the end of a token name. For instance $ClassName.CamelCase$
Name | Description | Example |
---|---|---|
CamelCase | Gives a camelCased version of the value. |
LoggerService -> loggerService |
NewLineIfPopulated | Adds a newline at the end if the original value was non-empty. | SomeTokenValue -> SomeTokenValue<newline> |