-
Notifications
You must be signed in to change notification settings - Fork 17
Custom Format Tokens
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 |
---|---|---|
$$ | Escape sequence. |
$$"String with {variables}" -> $"String with {variables}" |
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) |
Mock dependency 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 |
TestedMethodReturnType | The name of the MUT's return type. | Task<List<RandomResponse>> |
HttpType | If the MUT is an ASP.NET MVC endpoint, specifies the first REST type Attribute decorating it. You should use the ReplaceOneFullMatch Token Modifier to convert this output to the name of the C# function you want to use to test each type of REST endpoint implemented by your SUT. |
If the tested method has [HttpPost] annotation -> POST If it has no Http annotation -> "NONE" |
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) |
MethodParameters | Parameters used to invoke the MUT. | firstNumber, secondNumber |
HttpMethodParameters | Parameter used to invoke an API Post method. This will only be the first parameter if there are any. |
If the Method is an HTTPPost method -> ComplexParameter If not Http or not a Post method-> blank |
UriParameters | Query parameters to add to a URI, if any. HTTPPost with only 1 parameter is not added to the query string. HTTPPost with more than one parameters are included in hope that they will all be simple types, which can be passed in a query string. |
If the tested method is HTTPPost with only one parameter or has no parameters -> <blank> otherwise -> ?firstNumber={firstNumber}&secondNumber={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> |
Remove | Removes the given string from the original value if found. |
TestedClassController -> TestedClass |
RemoveGeneric | Removes the named Generic wrapper from the original value if found. |
$TestedMethodReturnType .RemoveGeneric(ActionResult)$ Task<ActionResult<bool>> -> Task<bool> |
LowerCase | Gives an all lower case version of the value. |
LoggerService -> loggerservice |
Prefix | Adds the given text before the token if the token value is not empty. |
$HttpMethodParameters .Prefix(,)$ ComplexObject -> ,ComplexObject |
SingleLine | Converts multiline value into a single line value. |
arg1, arg2, arg3 -> arg1, arg2, arg3 |
Map | Takes one or more key value pair arguments and looks for the first key that matches the token (case insensitive). If found, changes the token to be the corresponding value. |
$HttpType.Map (GET=GetAsync,POST=PostAsJsonAsync)$ GET -> GetAsync POST -> PostAsJsonAsync |