Programmatically skip / ignore tests in MSTest v2

Programmatically skip / ignore tests in MSTest v2

Extending Matt Kotsenas's work

·

1 min read

We recently inherited a new service that had failing unit and integration tests due to some required configuration. Some of them were due to ASPNETCORE_ENVIRONMENT. I wanted to skip those tests according to the environment set. Looks like Matt Kotsenas had a similar need in his blog post.

Abstracting IgnoreIfAttribute

His code is flexible and allows the user to pass in a method that returns a boolean based on custom criteria. For my needs, I wanted the criteria abstracted since I didn't want to duplicate the definition or reference the common criteria (checking ASPNETCORE_ENVIRONMENT).

Now we just need to update the TestMethodWithIgnoreIfSupportAttribute to support this abstraction through polymorphism as seen above.

Used like so:

[TestCategory("Integration")]
[TestMethodWithIgnoreIfSupport]
[IgnoreIfEnvironmentIs("Local,Development")]
public async Task MethodUnderTest_Scenario_SomeExpectedResult
{
...
}

Separate Note:

Would be nice to be able to reference individual files from a gist.

%[https://gist.github.com/Wind010/439249944caed68619e6626f67b48fb8#file-baseignoreifattribute-cs]

or

%[https://gist.github.com/Wind010/439249944caed68619e6626f67b48fb8?file=baseignoreifattribute.cs]

Neither work

References