Solving the Infamous VSTest.Console.exe Error: A Step-by-Step Guide
Image by Keaton - hkhazo.biz.id

Solving the Infamous VSTest.Console.exe Error: A Step-by-Step Guide

Posted on

If you’re like many developers, you’ve likely encountered the frustrating error message “Method not found: ‘Void Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestSourceHost” when attempting to run unit tests via vstest.console.exe. This error can be a real showstopper, halting your development progress and leaving you scratching your head. Fear not, dear reader, for we’ve got the solution right here!

What’s Causing the Error?

Before we dive into the fix, let’s take a brief moment to understand the root cause of this error. The issue lies in the incompatibility between the vstest.console.exe version and the MSTestAdapter.dll file. The older versions of vstest.console.exe are not compatible with the newer versions of MSTestAdapter.dll, resulting in this error.

Verify Your Versions

To confirm this, let’s check the versions of vstest.console.exe and MSTestAdapter.dll on your system:

<path>\vstest.console.exe /version
<path>\MSTestAdapter.dll /version

Take note of the version numbers. If your vstest.console.exe is older than 15.9.0, you’ll need to update it to a compatible version.

The Fix: Update vstest.console.exe and MSTestAdapter.dll

Now that we’ve identified the issue, let’s get to the solution! Follow these steps to update vstest.console.exe and MSTestAdapter.dll:

  1. Open the Visual Studio Installer and select the “More” dropdown next to your installed version of Visual Studio.

  2. Select “Modify” and then click “individual components” in the Visual Studio installer.

  3. In the “Individual components” tab, scroll down to the “Code tools” section and select “Visual Studio Test Platform” (make sure the version is 15.9.0 or higher).

  4. Click “Modify” to update the Visual Studio Test Platform component.

  5. Once the update is complete, navigate to the directory where vstest.console.exe is located (typically <Program Files (x86)>\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\TestWindow).

  6. Verify the version of vstest.console.exe has been updated to 15.9.0 or higher by running the command:

    vstest.console.exe /version

  7. Update MSTestAdapter.dll by installing the MSTest.TestAdapter NuGet package:

        Install-Package MSTest.TestAdapter -Version 2.1.0
        
  8. Verify the version of MSTestAdapter.dll has been updated to 2.1.0 or higher by running the command:

    <path>\MSTestAdapter.dll /version

Troubleshooting Tips

If you’re still encountering issues after updating vstest.console.exe and MSTestAdapter.dll, try the following troubleshooting steps:

  • Ensure that the MSTestAdapter.dll file is in the same directory as vstest.console.exe or in a directory listed in the system’s PATH environment variable.

  • Check if there are multiple versions of vstest.console.exe on your system. If so, ensure you’re using the updated version.

  • Verify that your test project is configured to use the correct test adapter. In Visual Studio, go to “Test” > “Test Settings” > “Default Processor Architecture” and select the correct architecture (e.g., x86 or x64).

Running Unit Tests with vstest.console.exe

Now that we’ve resolved the issue, let’s run those unit tests! You can use the following command to execute your tests via vstest.console.exe:

vstest.console.exe <Test Assembly>.dll /TestCaseFilter:<TestCategory>

Replace <Test Assembly> with the name of your test assembly and <TestCategory> with the category of tests you want to run.

Command Option Description
/TestCaseFilter: Specifies the test category or name to filter the tests.
/Logger: Specifies the logger to use for test output (e.g., console, trx, or csv).
/Targets: Specifies the test assemblies to run.

For example, to run all tests in the “UnitTests” category, you would use the following command:

vstest.console.exe MyTests.dll /TestCaseFilter:UnitTests

Conclusion

With these steps, you should now be able to successfully run unit tests via vstest.console.exe without encountering the “Method not found: ‘Void Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestSourceHost” error. Remember to update vstest.console.exe and MSTestAdapter.dll to compatible versions, verify your versions, and troubleshoot any remaining issues.

Happy testing!

 

Keywords: vstest.console.exe, MSTestAdapter, unit testing, Visual Studio Test Platform, test adapter, test assembly.

Frequently Asked Question

Stuck with unit tests via vstest.console.exe? We’ve got you covered! Here are some answers to common questions about the “Method not found” error.

What is the “Method not found” error when running unit tests via vstest.console.exe?

This error occurs when the MSTest adapter is not properly configured or is missing from the test project. The error message “Method not found: ‘Void Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestSourceHost” indicates that the adapter is not able to find the required assembly.

How do I resolve the “Method not found” error when running unit tests via vstest.console.exe?

To resolve this error, ensure that the MSTest adapter is installed and configured correctly in your test project. You can do this by checking the NuGet package manager for the MSTest adapter and updating it to the latest version. Additionally, make sure that the adapter is correctly referenced in your test project.

What are the common causes of the “Method not found” error when running unit tests via vstest.console.exe?

Common causes of this error include an outdated or missing MSTest adapter, incorrect configuration of the adapter, and conflicts with other NuGet packages. Additionally, issues with the test project or the vstest.console.exe configuration can also lead to this error.

Can I use a different testing framework to run my unit tests instead of MSTest?

Yes, you can use other testing frameworks such as NUnit or xUnit to run your unit tests. These frameworks have their own adapters and configurations, so you’ll need to ensure that you’re using the correct adapter and configuration for your chosen framework.

How do I check if the MSTest adapter is installed and configured correctly in my test project?

You can check if the MSTest adapter is installed and configured correctly by checking the NuGet package manager for the adapter and verifying that it’s referenced correctly in your test project. You can also check the test project’s csproj file to ensure that the adapter is correctly configured.