Skip to content

Commit

Permalink
Detect parameters in mstest (#1378)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Dec 22, 2024
1 parent ffd2543 commit ceacf08
Show file tree
Hide file tree
Showing 15 changed files with 30 additions and 114 deletions.
2 changes: 1 addition & 1 deletion docs/explicit-targets.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public Task WithTargets() =>
name: "targetName")
]);
```
<sup><a href='/src/Verify.MSTest.Tests/Tests.cs#L130-L147' title='Snippet source file'>snippet source</a> | <a href='#snippet-ExplicitTargetsMSTest' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.MSTest.Tests/Tests.cs#L125-L142' title='Snippet source file'>snippet source</a> | <a href='#snippet-ExplicitTargetsMSTest' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
13 changes: 1 addition & 12 deletions docs/mdsource/parameterised-mstest.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

`UseParameters()` controls what parameters are used when naming files.

Verify.MSTest does not detect the parametrised arguments, as such `UseParameters()` is required.

snippet: UseParametersMSTest
Verify.MSTest automatically detects the method parameters. So `UseParameters()` is not required unless using custom parameters.

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`

Expand All @@ -18,22 +16,13 @@ If the number of parameters passed to `UseParameters()` is greater than the numb

### DataRow


#### Instance

snippet: DataRowInstanceMSTest


#### Fluent

snippet: DataRowFluentMSTest


## Overriding text used for parameters

include: override-parameters-text


snippet: UseTextForParametersMSTest


Expand Down
58 changes: 9 additions & 49 deletions docs/parameterised-mstest.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,7 @@ To change this file edit the source file and then run MarkdownSnippets.

`UseParameters()` controls what parameters are used when naming files.

Verify.MSTest does not detect the parametrised arguments, as such `UseParameters()` is required.

<!-- snippet: UseParametersMSTest -->
<a id='snippet-UseParametersMSTest'></a>
```cs
[DataTestMethod]
[DataRow("Value1")]
[DataRow("Value2")]
public Task UseParametersUsage(string arg)
{
var somethingToVerify = $"{arg} some text";
return Verify(somethingToVerify)
.UseParameters(arg);
}
```
<sup><a href='/src/Verify.MSTest.Tests/Snippets/ParametersSample.cs#L131-L143' title='Snippet source file'>snippet source</a> | <a href='#snippet-UseParametersMSTest' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
Verify.MSTest automatically detects the method parameters. So `UseParameters()` is not required unless using custom parameters.

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`

Expand All @@ -44,47 +28,24 @@ public Task UseParametersSubSet(string arg1, string arg2, string arg3)
.UseParameters(arg1, arg2);
}
```
<sup><a href='/src/Verify.MSTest.Tests/Snippets/ParametersSample.cs#L145-L156' title='Snippet source file'>snippet source</a> | <a href='#snippet-UseParametersSubSetMSTest' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.MSTest.Tests/Snippets/ParametersSample.cs#L114-L125' title='Snippet source file'>snippet source</a> | <a href='#snippet-UseParametersSubSetMSTest' 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.


### DataRow


#### Instance

<!-- snippet: DataRowInstanceMSTest -->
<a id='snippet-DataRowInstanceMSTest'></a>
```cs
[DataTestMethod]
[DataRow("Value1")]
[DataRow("Value2")]
public Task DataRowUsage(string arg)
{
var settings = new VerifySettings();
settings.UseParameters(arg);
return Verify(arg, settings);
}
```
<sup><a href='/src/Verify.MSTest.Tests/Snippets/ParametersSample.cs#L14-L26' title='Snippet source file'>snippet source</a> | <a href='#snippet-DataRowInstanceMSTest' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


#### Fluent

<!-- snippet: DataRowFluentMSTest -->
<a id='snippet-DataRowFluentMSTest'></a>
```cs
[DataTestMethod]
[DataRow("Value1")]
[DataRow("Value2")]
public Task DataRowUsageFluent(string arg) =>
Verify(arg)
.UseParameters(arg);
public Task DataRowUsage(string arg) =>
Verify(arg);
```
<sup><a href='/src/Verify.MSTest.Tests/Snippets/ParametersSample.cs#L28-L37' title='Snippet source file'>snippet source</a> | <a href='#snippet-DataRowFluentMSTest' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.MSTest.Tests/Snippets/ParametersSample.cs#L14-L22' title='Snippet source file'>snippet source</a> | <a href='#snippet-DataRowInstanceMSTest' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand All @@ -108,7 +69,6 @@ For the fluent case:
* TheTest.UseTextForParametersFluent_Value1.verified.txt
* TheTest.UseTextForParametersFluent_Value2.verified.txt<!-- endInclude -->


<!-- snippet: UseTextForParametersMSTest -->
<a id='snippet-UseTextForParametersMSTest'></a>
```cs
Expand Down Expand Up @@ -158,7 +118,7 @@ public Task IgnoreParametersForVerified(string arg)
return Verify("value", settings);
}
```
<sup><a href='/src/Verify.MSTest.Tests/Snippets/ParametersSample.cs#L39-L51' title='Snippet source file'>snippet source</a> | <a href='#snippet-IgnoreParametersForVerifiedMSTest' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.MSTest.Tests/Snippets/ParametersSample.cs#L24-L36' title='Snippet source file'>snippet source</a> | <a href='#snippet-IgnoreParametersForVerifiedMSTest' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand All @@ -174,7 +134,7 @@ public Task IgnoreParametersForVerifiedFluent(string arg) =>
Verify("value")
.IgnoreParametersForVerified(arg);
```
<sup><a href='/src/Verify.MSTest.Tests/Snippets/ParametersSample.cs#L53-L62' title='Snippet source file'>snippet source</a> | <a href='#snippet-IgnoreParametersForVerifiedFluentMSTest' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.MSTest.Tests/Snippets/ParametersSample.cs#L38-L47' title='Snippet source file'>snippet source</a> | <a href='#snippet-IgnoreParametersForVerifiedFluentMSTest' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand All @@ -198,7 +158,7 @@ public Task IgnoreParametersForVerifiedCustomParams(string arg)
return Verify("value", settings);
}
```
<sup><a href='/src/Verify.MSTest.Tests/Snippets/ParametersSample.cs#L64-L76' title='Snippet source file'>snippet source</a> | <a href='#snippet-IgnoreParametersForVerifiedCustomParamsMSTest' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.MSTest.Tests/Snippets/ParametersSample.cs#L49-L61' title='Snippet source file'>snippet source</a> | <a href='#snippet-IgnoreParametersForVerifiedCustomParamsMSTest' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand All @@ -214,7 +174,7 @@ public Task IgnoreParametersForVerifiedFluentCustomParams(string arg) =>
Verify("value")
.IgnoreParametersForVerified($"Number{arg}");
```
<sup><a href='/src/Verify.MSTest.Tests/Snippets/ParametersSample.cs#L78-L87' title='Snippet source file'>snippet source</a> | <a href='#snippet-IgnoreParametersForVerifiedCustomParamsFluentMSTest' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify.MSTest.Tests/Snippets/ParametersSample.cs#L63-L72' title='Snippet source file'>snippet source</a> | <a href='#snippet-IgnoreParametersForVerifiedCustomParamsFluentMSTest' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CA1822;CS1591;CS0649;xUnit1026;xUnit1013;CS1573;VerifyTestsProjectDir;VerifySetParameters;PolyFillTargetsForNuget</NoWarn>
<Version>28.6.1</Version>
<Version>28.7.0</Version>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>preview</LangVersion>
<AssemblyVersion>1.0.0</AssemblyVersion>
Expand Down
4 changes: 2 additions & 2 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageVersion Include="NUnit" Version="4.3.0" />
<PackageVersion Include="MSTest" Version="3.6.4" />
<PackageVersion Include="MSTest.TestFramework" Version="3.6.4" />
<PackageVersion Include="MSTest" Version="3.7.0" />
<PackageVersion Include="MSTest.TestFramework" Version="3.7.0" />
<PackageVersion Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageVersion Include="Polyfill" Version="7.8.0" />
<PackageVersion Include="ProjectDefaults" Version="1.0.147" />
Expand Down
2 changes: 0 additions & 2 deletions src/Verify.MSTest.Tests/Snippets/ParametersHashSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public partial class ParametersHashSample
public Task HashParametersUsage(string arg)
{
var settings = new VerifySettings();
settings.UseParameters(arg);
settings.HashParameters();
return Verify(arg, settings);
}
Expand All @@ -19,7 +18,6 @@ public Task HashParametersUsage(string arg)
[DataRow("Value2")]
public Task HashParametersUsageFluent(string arg) =>
Verify(arg)
.UseParameters(arg)
.HashParameters();
}

Expand Down

This file was deleted.

This file was deleted.

35 changes: 2 additions & 33 deletions src/Verify.MSTest.Tests/Snippets/ParametersSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,8 @@ public Task UseTextForParameters(string arg) =>
[DataTestMethod]
[DataRow("Value1")]
[DataRow("Value2")]
public Task DataRowUsage(string arg)
{
var settings = new VerifySettings();
settings.UseParameters(arg);
return Verify(arg, settings);
}

#endregion

#region DataRowFluentMSTest

[DataTestMethod]
[DataRow("Value1")]
[DataRow("Value2")]
public Task DataRowUsageFluent(string arg) =>
Verify(arg)
.UseParameters(arg);
public Task DataRowUsage(string arg) =>
Verify(arg);

#endregion

Expand Down Expand Up @@ -94,7 +79,6 @@ public Task IgnoreParametersForVerifiedFluentCustomParams(string arg) =>
public Task IgnoreParameters(string arg)
{
var settings = new VerifySettings();
settings.UseParameters(arg);
settings.IgnoreParameters(nameof(arg));
return Verify("value", settings);
}
Expand All @@ -108,7 +92,6 @@ public Task IgnoreParameters(string arg)
[DataRow("Two")]
public Task IgnoreParametersFluent(string arg) =>
Verify("value")
.UseParameters(arg)
.IgnoreParameters(nameof(arg));

#endregion
Expand All @@ -128,20 +111,6 @@ public Task IgnoreParametersCustomParams(string arg)

#endregion

#region UseParametersMSTest

[DataTestMethod]
[DataRow("Value1")]
[DataRow("Value2")]
public Task UseParametersUsage(string arg)
{
var somethingToVerify = $"{arg} some text";
return Verify(somethingToVerify)
.UseParameters(arg);
}

#endregion

#region UseParametersSubSetMSTest

[DataTestMethod]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

9 changes: 3 additions & 6 deletions src/Verify.MSTest.Tests/Snippets/ParametersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@ public partial class ParametersTests
//[DataRow("1.1")]
//public async Task Decimal(decimal arg)
//{
// await Verify(arg)
// .UseParameters(arg);
// await Verify(arg);
//}

[DataTestMethod]
[DataRow((float) 1.1)]
public Task Float(float arg) =>
Verify(arg)
.UseParameters(arg);
Verify(arg);

[DataTestMethod]
[DataRow(1.1d)]
public Task Double(double arg) =>
Verify(arg)
.UseParameters(arg);
Verify(arg);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

5 changes: 0 additions & 5 deletions src/Verify.MSTest.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ static void DerivePathInfo()
#endregion
}

[DataTestMethod]
[DataRow("Value1")]
public Task MissingParameter(string arg) =>
Verify("Foo");

[DataTestMethod]
[DataRow("Value1")]
public Task UseFileNameWithParam(string arg) =>
Expand Down
9 changes: 9 additions & 0 deletions src/Verify.MSTest/Verifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ static InnerVerifier BuildVerifier(VerifySettings settings, string sourceFile, b
var type = context.TestClass;
var method = context.Method;

if (!settings.HasParameters)
{
var data = context.TestContext.TestData;
if (data != null)
{
settings.SetParameters(data);
}
}

VerifierSettings.AssignTargetAssembly(assembly);
var pathInfo = GetPathInfo(sourceFile, type, method);
return new(
Expand Down

0 comments on commit ceacf08

Please sign in to comment.