14

如何使用.testsettings文件运行单元测试vstest.console.exe?我创建了空的 Visual Studio 解决方案,创建了空的单元测试项目,将Local.testsettings文件添加为解决方案项。

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {

    }
}

在此处输入图像描述

<?xml version="1.0" encoding="UTF-8"?>
<TestSettings name="Local" id="1109524d-9809-4423-b7fa-fad429ebfd8d" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
  <Description>These are default test settings for a local test run.</Description>
  <Deployment enabled="false" />
  <Execution hostProcessPlatform="MSIL">
    <TestTypeSpecific>
      <UnitTestRunConfig testTypeId="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b">
        <AssemblyResolution>
          <TestDirectory useLoadContext="true" />
        </AssemblyResolution>
      </UnitTestRunConfig>
    </TestTypeSpecific>
    <AgentRule name="LocalMachineDefaultRole">
    </AgentRule>
  </Execution>
  <Properties />
</TestSettings>

当我使用以下命令运行测试时,一切正常:

>> "[path to vstest]/vstest.console.exe" [path to project]\UnitTestProject1.dll

下面的命令给出了一个错误。

"[path to vstest]/vstest.console.exe" [path to project]\UnitTestProject1.dll /Settings:[path to settings file]\Local.testsettings

警告:MSTest V2 适配器不支持将 ForcedLegacyMode 设置为 true 的 testsettings 文件或 runsettings。[路径]\UnitTestProject1.dll 中没有可用的测试。确保测试发现者和执行者已注册并且平台和框架版本设置正确,然后重试。

此外,可以使用 /TestAdapterPath 命令指定测试适配器的路径。示例 /TestAdapterPath:。

所以我添加了/TestAdapterPath:[project path/bin/Debug]参数。with 发现者和执行Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.dll者位于此处。但是没有关于指定测试适配器的最后一句话,我得到了同样的错误。

我想知道是否有人可以解决这个问题。

4

2 回答 2

42

我有类似的问题,我已经通过以下方式解决了它:

  1. 转到测试-> 测试设置

  2. 取消选中\..\..projectTestSettings.testsettings文件

  3. 再次运行测试

于 2018-11-01T20:03:23.593 回答
15

解决方案是默认Microsoft.VisualStudio.QualityTools.UnitTestFramework使用Microsoft.VisualStudio.TestPlatform.TestFrameworkVisual Studio 将其添加到您的单元测试项目中。因此,您可以通过NuGet. 您应该删除MSTest.TestAdapterMSTest.TestFramework安装Microsoft.VisualStudio.QualityTools.UnitTestFramework.Updated. 您的单元测试将在这些步骤之后被发现。

您还可以阅读以下有关测试框架MSTest V2的有用文章。

于 2018-04-11T11:09:14.950 回答