gpt4 book ai didi

c# - 从命令提示符运行单元测试失败

转载 作者:太空狗 更新时间:2023-10-30 01:20:36 26 4
gpt4 key购买 nike

目前我正在努力处理一些在 visual studio 中运行良好但在 Teamcity 中失败的单元测试

我将问题追踪到 mstests.exe

假设我执行以下步骤:

  1. 创建一个新的测试项目
  2. 使用以下测试添加一个新的测试类

    [TestMethod]
    public void TestCanCreateSqLiteConnection()
    {
    // Create the DbProviderFactory
    var factory = DbProviderFactories.GetFactory("System.Data.SQLite");
    // Create the DbConnection.
    var connection = factory.CreateConnection();
    // Assign connection string
    connection.ConnectionString = "Data Source=database.sqlite";
    // check the result
    Assert.IsTrue(connection.GetType().Name.Equals("SQLiteConnection"));
    }
  3. 添加一个 app.config 文件并添加:

    <system.data>
    <DbProviderFactories>

    <remove invariant="System.Data.SQLite" />
    <add name="SQLite Data Provider"
    invariant="System.Data.SQLite"
    description=".Net Framework Data Provider for SQLite"
    type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />

    </DbProviderFactories>
    </system.data>
  4. 通过 nuget 安装“System.Data.SQLite (x86/x64)”

  5. 从 Visual Studio (2010) 运行测试。它应该运行良好:

现在我想通过 mstest.exe 运行相同的测试,所以我:

  1. 打开 Visual Studio 2010 命令提示符

  2. 导航到 bin\debug 文件夹

  3. 执行

    mstest.exe /testcontainer:TestProject1.dll /detail:errormessage
  4. 测试最终失败

    System.DllNotFoundException: Unable to load DLL 'SQLite.Interop.DLL':
    The specified module could not be found. (Exception from HRESULT:0x8007007E)
  5. 现在,如果我使用测试设置扩展对 mstest.exe 的调用,则测试运行正常。

    mstest.exe /testcontainer:TestProject1.dll /detail:errormessage 
    testsettings:..\..\..\Local.testsettings

Local.testsettings 没有什么特别之处,即使我创建一个新的 testsettings 文件并使用它,测试也会通过。

    <?xml version="1.0" encoding="UTF-8"?>
<TestSettings id="fc837936-41d1-4987-8526-34f9336569f5" name="TestSettings1" enableDefaultDataCollectors="false" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
<Description>default test run</Description>
<Deployment enabled="false"/>
</TestSettings>

所以主要问题是,为什么这会影响我的测试运行,以及我如何在不指定 *.testsettings 文件的情况下从命令行运行我的测试。

最佳答案

您需要使用 DeploymentItem 来确保在通过命令行进行测试时将文件复制到部署目录。我为所有依赖于 SQLite 数据库的测试类创建了一个基类。

[TestClass]
[DeploymentItem("Resources\\empty-db.sqlite", "Resources")]
[DeploymentItem("x64\\SQLite.Interop.dll", "x64")]
[DeploymentItem("x86\\SQLite.Interop.dll", "x86")]
public class SQLiteTest
{
[TestInitialize()]
public void ClearDatabase()
{
File.Copy("Resources\\empty-db.sqlite", "test-db.sqlite", true);
}
}

关于c# - 从命令提示符运行单元测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18591381/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com