Verify/docs/parameterised-fixie.md

214 строки
8.3 KiB
Markdown

<!--
GENERATED FILE - DO NOT EDIT
This file was generated by [MarkdownSnippets](https://github.com/SimonCropp/MarkdownSnippets).
Source File: /docs/mdsource/parameterised-fixie.source.md
To change this file edit the source file and then run MarkdownSnippets.
-->
# Fixie Parameterised Tests
## UseParameters()
`UseParameters()` controls what parameters are used when naming files.
Verify.Fixie automatically detects the method parameters via a [custom ITestProject](#custom-parameterisation).
### Usage:
<!-- snippet: UseParametersFixie -->
<a id='snippet-UseParametersFixie'></a>
```cs
[TestCase("Value1")]
[TestCase("Value2")]
public Task UseParametersUsage(string arg)
{
var somethingToVerify = $"{arg} some text";
return Verify(somethingToVerify)
.UseParameters(arg);
}
```
<sup><a href='/src/Verify.Fixie.Tests/Snippets/ParametersSample.cs#L19-L30' title='Snippet source file'>snippet source</a> | <a href='#snippet-UseParametersFixie' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
If not all parameters are required, a subset can be passed in. In this scenario, the parameters passed in will match with the method parameter names from the start. For example the following will result in a file named `ParametersSample.UseParametersSubSet_arg1=Value1_arg2=Value2.verified.txt`
<!-- snippet: UseParametersSubSetFixie -->
<a id='snippet-UseParametersSubSetFixie'></a>
```cs
[TestCase("Value1", "Value2", "Value3")]
public Task UseParametersSubSet(string arg1, string arg2, string arg3)
{
var somethingToVerify = $"{arg1} {arg2} {arg3} some text";
return Verify(somethingToVerify)
.UseParameters(arg1, arg2);
}
```
<sup><a href='/src/Verify.Fixie.Tests/Snippets/ParametersSample.cs#L55-L65' title='Snippet source file'>snippet source</a> | <a href='#snippet-UseParametersSubSetFixie' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
If the number of parameters passed to `UseParameters()` is greater than the number of parameters in the test method, an exception will be thrown.
## Custom parameterisation
Fixie has no build in test parameterisation. Test parameterisation need to be implemented by the consuming library. See [Attribute-Based Parameterization](https://github.com/fixie/fixie/wiki/Customizing-the-Test-Project-Lifecycle#recipe-attribute-based-parameterization) for an example.
Verify.Fixie requires some customisation of the above example.
* Inside `ITestProject.Configure` call `VerifierSettings.AssignTargetAssembly(environment.Assembly);`
* Inside `IExecution.Run` wrap `test.Run` in `using (ExecutionState.Set(testClass, test, parameters))`
Example implementation:
<!-- snippet: TestProject.cs -->
<a id='snippet-TestProject.cs'></a>
```cs
public class TestProject :
ITestProject,
IExecution
{
public void Configure(TestConfiguration configuration, TestEnvironment environment)
{
VerifierSettings.AssignTargetAssembly(environment.Assembly);
configuration.Conventions.Add<DefaultDiscovery, TestProject>();
}
public async Task Run(TestSuite testSuite)
{
foreach (var testClass in testSuite.TestClasses)
{
foreach (var test in testClass.Tests)
{
if (test.HasParameters)
{
foreach (var parameters in test
.GetAll<TestCase>()
.Select(_ => _.Parameters))
{
using (ExecutionState.Set(testClass, test, parameters))
{
await test.Run(parameters);
}
}
}
else
{
using (ExecutionState.Set(testClass, test, null))
{
await test.Run();
}
}
}
}
}
}
```
<sup><a href='/src/Verify.Fixie.Tests/FixieSetup/TestProject.cs#L1-L39' title='Snippet source file'>snippet source</a> | <a href='#snippet-TestProject.cs' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
Resulting usage:
<!-- snippet: TestCaseFixie -->
<a id='snippet-TestCaseFixie'></a>
```cs
[TestCase("Value1")]
[TestCase("Value2")]
public Task TestCaseUsage(string arg) =>
Verify(arg);
```
<sup><a href='/src/Verify.Fixie.Tests/Snippets/ParametersSample.cs#L67-L74' title='Snippet source file'>snippet source</a> | <a href='#snippet-TestCaseFixie' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
## Overriding text used for parameters
`UseTextForParameters()` can be used to override the substitution text used for the `{Parameters}` part of the file convention.<!-- include: override-parameters-text. path: /docs/mdsource/override-parameters-text.include.md -->
```
{Directory}/{TestClassName}.{TestMethodName}_{Parameters}_{UniqueFor1}_{UniqueFor2}_{UniqueForX}.verified.{extension}
```
The below samples produce:
For the instance case:
* TheTest.UseTextForParameters_Value1.verified.txt
* TheTest.UseTextForParameters_Value2.verified.txt
For the fluent case:
* TheTest.UseTextForParametersFluent_Value1.verified.txt
* TheTest.UseTextForParametersFluent_Value2.verified.txt<!-- endInclude -->
### Instance
<!-- snippet: UseTextForParametersInstanceFixie -->
<a id='snippet-UseTextForParametersInstanceFixie'></a>
```cs
[TestCase("Value1")]
[TestCase("Value2")]
public Task UseTextForParameters(string arg)
{
var settings = new VerifySettings();
settings.UseTextForParameters(arg);
return Verify(arg + "UseTextForParameters", settings);
}
```
<sup><a href='/src/Verify.Fixie.Tests/Snippets/ParametersSample.cs#L32-L43' title='Snippet source file'>snippet source</a> | <a href='#snippet-UseTextForParametersInstanceFixie' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
### Fluent
<!-- snippet: UseTextForParametersFluentFixie -->
<a id='snippet-UseTextForParametersFluentFixie'></a>
```cs
[TestCase("Value1")]
[TestCase("Value2")]
public Task UseTextForParametersFluent(string arg) =>
Verify(arg + "UseTextForParametersFluent")
.UseTextForParameters(arg);
```
<sup><a href='/src/Verify.Fixie.Tests/Snippets/ParametersSample.cs#L45-L53' title='Snippet source file'>snippet source</a> | <a href='#snippet-UseTextForParametersFluentFixie' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
## Ignore parameters for verified filename
By default, Verify expects every parameterized case to have a unique [file name](/docs/naming.md) with the parameters appended to the file name. This behavior can be overridden by using `IgnoreParametersForVerified()`. In this case, the verified file name does not contain the parameter values, meaning it is the same for each testcase.<!-- include: ignore-parameters. path: /docs/mdsource/ignore-parameters.include.md -->
`IgnoreParametersForVerified` accepts an array for passing through the parameters. These values are passed to [UseParameters](#UseParameters). This is required for MSTest, and xUnit. Parameters should not be passed for NUnit, TUnit and Fixie since they are automatically detected.
The below samples produce:
For the instance case:
* NamerTests.IgnoreParametersForVerified_arg=One.received.txt
* NamerTests.IgnoreParametersForVerified_arg=Two.received.txt
* NamerTests.IgnoreParametersForVerified.verified.txt
For the fluent case:
* NamerTests.IgnoreParametersForVerifiedFluent_arg=One.received.txt
* NamerTests.IgnoreParametersForVerifiedFluent_arg=Two.received.txt
* NamerTests.IgnoreParametersForVerifiedFluent.verified.txt<!-- endInclude -->
## IgnoreParametersForVerified with override parameters
The parameters passed to IgnoreParametersForVerified can be used pass custom parameters to [UseParameters](#UseParameters).
## Hashing parameters
Parameters can be hashed as an alternative to being stringified. This is useful when the parameters are large and could potentially generate file names that exceed allowances of the OS.<!-- include: hashing-parameters. path: /docs/mdsource/hashing-parameters.include.md -->
Hashing parameter is achieved by using `HashParameters`.
[Overriding text used for parameters](#overriding-text-used-for-parameters) is respected when generating the hash.
[XxHash64](https://learn.microsoft.com/en-us/dotnet/api/system.io.hashing.xxhash64) is used to perform the hash.<!-- endInclude -->