目前,我正在努力解决一些在 Visual Studio 中运行良好但在 Teamcity 中失败的单元测试
我将问题追踪到 mstests.exe
假设我执行以下步骤:
- 创建一个新的测试项目
使用以下测试添加一个新的测试类
[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")); }
添加一个 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>
通过 nuget 安装“System.Data.SQLite (x86/x64)”
从 Visual Studio (2010) 运行测试。它应该运行良好:
现在我想通过 mstest.exe 运行相同的测试,所以我:
打开 Visual Studio 2010 命令提示符
导航到 bin\debug 文件夹
执行
mstest.exe /testcontainer:TestProject1.dll /detail:errormessage
测试最终失败
System.DllNotFoundException: Unable to load DLL 'SQLite.Interop.DLL': The specified module could not be found. (Exception from HRESULT:0x8007007E)
现在,如果我使用 testsettings 扩展对 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 文件的情况下从命令行运行测试。