Keywords: Resharper | MSTest | Unit Testing
Abstract: This article addresses the common "Test wasn't run" error in C# unit testing, focusing on integration issues between Resharper and MSTest. Based on the best solution—disabling Resharper's legacy MSTest runner—and supplemented by other factors like async method return types, assembly shadow-copying, and corrupted configuration files, it provides a comprehensive troubleshooting guide. Structured as a technical paper, it covers problem reproduction, core solutions, supplementary causes, and preventive measures to help developers efficiently resolve test execution barriers.
Problem Background and Reproduction
In C# development environments, when using the MSTest framework for unit testing, developers often encounter test methods displaying "Test wasn't run" or "Inconclusive" statuses, preventing normal execution. This issue typically arises when integrating development environments (e.g., Visual Studio) with Resharper tools. Below is a typical problematic code example:
namespace AdminPortal.Tests.Controller_Test.Customer
{
[TestClass]
public class BusinessUnitControllerTests
{
private IBusinessUnitRepository _mockBusinessUnitRepository;
private BusinessUnitController _controller;
[TestInitialize]
public void TestInitialize()
{
_mockBusinessUnitRepository = MockRepository.GenerateMock<IBusinessUnitRepository>();
_controller = new BusinessUnitController(_mockBusinessUnitRepository);
}
[TestMethod]
public void Index_Action_Calls_GetAllBusinessUnit()
{
_mockBusinessUnitRepository.Stub(x => x.GetAllBusinessUnit());
_controller.Index();
_mockBusinessUnitRepository.AssertWasCalled(x=>x.GetAllBusinessUnit());
}
}
}When running such tests, the test results window may show error messages indicating that tests were not executed, despite correct code logic and configured project references. This issue not only affects test coverage but can also disrupt continuous integration workflows.
Core Solution: Disabling Resharper Legacy Runner
According to the best answer (Answer 4, score 10.0), the root cause of this problem lies in Resharper's MSTest integration settings. Resharper may default to enabling the "Use Legacy Runner" option, which can cause incompatibility with the latest versions of MSTest or the testing environment, thereby blocking test execution. The solution is as follows:
- Open the Resharper settings menu via: Resharper > Options > Tools > Unit Testing > MSTest.
- On the MSTest configuration page, locate the "Use Legacy Runner" checkbox.
- Uncheck this option to disable the legacy runner and enable Resharper's modern test running mechanism.
- Save the settings and rerun the test project. Typically, this action immediately resolves the "Test wasn't run" error, allowing tests to execute normally.
This method is based on common issues in Resharper 7 and above, but may vary slightly due to IDE version differences. After disabling the legacy runner, Resharper uses updated test adapters, improving compatibility with the MSTest framework and ensuring test methods are correctly identified and executed.
Supplementary Causes and Solutions
In addition to the core solution, other answers provide additional troubleshooting angles that can serve as supplementary references:
- Async Method Return Type Issue (Answer 2, score 4.2): If a test method uses the
asyncmodifier, it must return aTasktype, notvoid. For example, erroneous code likepublic async void MyTest()should be changed topublic async Task MyTest(). This is because MSTest framework support for async tests requires the correct return type; otherwise, tests may not run. - Assembly Shadow-Copy Setting (Answer 3, score 2.6): In Resharper's unit testing settings, the "Shadow-copy assemblies being tested" option can cause assembly loading issues. Unchecking this option prevents test assemblies from being copied to temporary directories, avoiding path or permission-related errors.
- Corrupted Configuration or Missing Dependencies (Answer 1, score 10.0): Check the test project's
App.configorweb.configfiles to ensure there are no corrupted entries or missing NuGet package references. For instance, if the configuration file references an uninstalled package, it may cause test initialization to fail. Fixing the configuration file or installing missing packages can resolve this.
In-Depth Analysis and Preventive Measures
To fundamentally avoid the "Test wasn't run" error, developers should adopt the following preventive measures:
- Environment Consistency Check: Ensure compatibility between Resharper, Visual Studio, and MSTest test adapter versions in the development environment. Regularly update the toolchain to avoid using outdated plugins or runners.
- Test Code Standards: Follow MSTest best practices, such as correctly using
[TestClass]and[TestMethod]attributes, and avoid unsupported return types (e.g.,async void) in test methods. For async tests, always useasync Taskreturns. - Configuration Management: Maintain clean configuration files to avoid syntax errors from manual edits. Use NuGet package manager to handle dependencies, ensuring all referenced packages are correctly installed and synchronized.
- Tool Settings Optimization: In team development, standardize Resharper settings, particularly unit testing-related options. This can be done by exporting settings files or using version control to share configurations, reducing issues caused by environmental differences.
By combining the core solution with supplementary measures, developers can not only quickly resolve current errors but also enhance the reliability and maintainability of their test suites. In practical projects, it is recommended to first try disabling the legacy runner; if the issue persists, gradually investigate other potential causes.