Reputation: 15327
I have 3 test projects, with the following dependency hierarchy:
Tests.Common (.NET Standard library)
Tests.DotNetCore (.NET Core library)
Tests.Wpf (.NET Framework library)
The idea is to define the test methods as calling into a RunTests
method in Tests.Common
:
// using xUnit
// using System.Linq.Expressions
// using static System.Linq.Expressions.Expression
namespace Tests.Common {
public class TestBase {
protected virtual void RunTest(Expression expr, string result) { ... }
}
public class Constructed {
[Fact]
public void ConstructBinary() =>
RunTest(
Add(Constant(5), Constant(42)),
"5 + 42"
);
}
}
and then in Tests.Wpf
I can test the same expressions against UI-only VM classes, by overriding the RunTests
method:
// using ExpressionToString.Wpf
namespace Tests.Wpf {
public class Constructed : Tests.Common.Constructed {
public override void RunTest(Expression expr, string result) {
var vm = new ExpressionVM(expr);
// at this point, all I want to test is that the VM class is created successfully
}
}
}
My problem is that Visual Studio Test Explorer discovers the test methods in Tests.Common
, even though xUnit tests in a .NET Standard library cannot be run; they require a platform-specific library (this is why I have a Tests.DotNetCore
test project, and an additional inheritance chain there.)
How can I prevent Visual Studio Test Explorer from displaying the tests in the .NET Standard library?
Upvotes: 3
Views: 621
Reputation: 8135
Declare the base class in the Common
project as abstract
.
That should make the test explorer exclude the base class as a tests class, and recognize the test methods in all descendant classes.
Upvotes: 2
Reputation: 3269
Do not set [Fact] attributes on them, but only on the actual implementations.
Test discovery logic is based on matching [Fact] attributes on methods with xUnit supported signatures in test projects. Do not use [Fact] attributes in top level implementation but only in the overriden methods which are used for running actual tests. This will prevent their detection at any level in both Visual Studio Test Explorer and in xUnit runners. If want to directly reuse top level method override it and call base.RunTest() in the body.
Upvotes: 0