我们有一些集成/单元测试来验证我们的NHibernate映射。我介绍了一些数据驱动的测试,这些测试利用测试方法上方的[DeploymentItem]
属性来导入CSV文件。在单元测试项目的任何测试中仅存在此属性就会导致所有 NHibernate 映射测试失败,并显示以下消息:
Error Message: Initialization method UnitTests.SearchOrganizationTests.Setup threw exception. NHibernate.HibernateException: NHibernate.HibernateException: Could not create the driver from NHibernate.Driver.OracleDataClientDriver. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.. Stack Trace: at NHibernate.Driver.OracleDataClientDriver..ctor() --- End of inner exception stack trace --- at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) at System.Activator.CreateInstance(Type type, Boolean nonPublic) at System.Activator.CreateInstance(Type type) at NHibernate.Bytecode.ActivatorObjectsFactory.CreateInstance(Type type) at NHibernate.Connection.ConnectionProvider.ConfigureDriver(IDictionary`2 settings) --- End of inner exception stack trace --- at NHibernate.Connection.ConnectionProvider.ConfigureDriver(IDictionary`2 settings) at NHibernate.Connection.ConnectionProvider.Configure(IDictionary`2 settings) at NHibernate.Connection.ConnectionProviderFactory.NewConnectionProvider(IDictionary`2 settings) at NHibernate.Cfg.SettingsFactory.BuildSettings(IDictionary`2 properties) at NHibernate.Cfg.Configuration.BuildSettings() at NHibernate.Cfg.Configuration.BuildSessionFactory() at MyApplication.DataContext.get_SessionFactory() at MyApplication.DataContext.GetClassMetadata(Type type) at MyApplication.DataContext.GetClassMetadata[T]() at MyApplication.DataContext.GetTableName[T]()
无论从命令行使用mstest.exe
还是vstest.console.exe
,我都会遇到相同的故障。
从Visual Studio 2013中的"测试资源管理器"运行测试就可以了。从命令行运行测试失败,这对我们的持续集成服务器来说是一个问题。
所以附加信息:
- 这是Visual Studio中的一个单元测试项目
- 仅当我不运行使用
[DeploymentItem]
属性修饰的测试时,Oracle.DataAccess.dll
文件才位于构建输出目录中(并且"复制本地"设置为"True") - 运行至少 1 个使用
[DeploymentItem]
属性修饰的测试时,Oracle.DataAccess.dll
文件不会复制到输出目录 - 所有 CSV 文件的"复制到输出"属性设置为"始终">
- Visual Studio 2013
- NHibernate 版本 4.0.4.4000
- .NET 4.5.1
- 流利NHibernate版本2.0.3.0
- 我的计算机上安装了 Oracle 客户端 12c (12.1.0)
更新#1:我查看了TestResults/<Test run name>/Out
目录,但没有看到Oracle.DataAccess.dll文件
更新 #2:我们有一个测试,它直接使用 Oracle 客户端库打开与 Oracle 数据库的连接。我在此测试上方添加了一个 [DeploymentItem] 属性,引用了 Oracle.DataAccess.dll 文件。现在,NHibernate映射测试已通过,但数据驱动测试未加载CSV文件。
[TestClass]
public class OracleConnectionTests
{
[TestMethod]
[TestProperty("Data Mappings", "Connection Test")]
[DeploymentItem(@"..DependenciesOracle.DataAccess.dll")]
public void CanOpenCloseConnection()
{
OracleConnection oraConn = new OracleConnection("connection string here");
oraConn.Open();
Assert.IsTrue(oraConn.State == ConnectionState.Open);
oraConn.Close();
Assert.IsFalse(oraConn.State == ConnectionState.Open);
}
}
数据驱动测试示例:
[TestClass]
public class FixedPrologueTests
{
private const string DATA_PROVIDER = "Microsoft.VisualStudio.TestTools.DataSource.CSV";
private const string DATA_FILE_PATH = @".UnitTestsViewModelTestsFixedPrologueTestData.csv";
private const string DATA_OUTPUT_PATH = @"UnitTestsViewModelTests";
private const string DATA_TABLE_NAME = "FixedPrologueTestData.csv";
public TestContext TestContext { get; set; }
private PaginationPrologueTestData data;
private PaginationPrologue prologue;
private System.Collections.Generic.IEnumerable<int> epilogue;
[TestInitialize]
public void Setup()
{
data = new PaginationPrologueTestData(TestContext.DataRow);
prologue = new PaginationPrologue(data.Padding, data.CurrentPage, data.TotalPages);
epilogue = prologue.CreateEpilogue();
}
[TestMethod]
[TestProperty("PaginationPrologue", "Prologue remains fixed when nearing end of result set")]
[DataSource(DATA_PROVIDER, DATA_FILE_PATH, DATA_TABLE_NAME, DataAccessMethod.Sequential)]
[DeploymentItem(DATA_FILE_PATH, DATA_OUTPUT_PATH)]
public void WhenNearingEndOfResultSet_EpilogueIsNotIncluded()
{
Assert.AreEqual(data.EpilogueLength, epilogue.Count());
}
如何包含 [DeploymentItem] 属性以允许数据驱动测试加载 CSV 文件,并允许 NHibernate 在从命令行运行测试时加载 Oracle 驱动程序?
我最终用编译器标志"修复"了这个问题:
[TestMethod]
[TestProperty("PaginationPrologue", "Prologue remains fixed when nearing end of result set")]
[DataSource(DATA_PROVIDER, DATA_FILE_PATH, DATA_TABLE_NAME, DataAccessMethod.Sequential)]
#IF DEPLOY_TEST_DATA
[DeploymentItem(DATA_FILE_PATH, DATA_OUTPUT_PATH)]
#ENDIF
public void WhenNearingEndOfResultSet_EpilogueIsNotIncluded()
{
Assert.AreEqual(data.EpilogueLength, epilogue.Count());
}
由于此错误仅在在我们的构建服务器上运行自动测试时发生,因此这是一个足够好的修复程序。老实说,我仍然不确定为什么这有效,我只知道它确实如此。已经3年多了,事情一直很稳定。