-1

我们有一些集成/单元测试来验证我们的 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 中的单元测试项目
  • Oracle.DataAccess.dll仅当我不运行使用该[DeploymentItem]属性修饰的测试时,该文件才位于构建输出目录中(并且“Copy Local”设置为“True”)
  • 运行至少 1 个使用该[DeploymentItem]属性装饰的测试时,Oracle.DataAccess.dll文件不会被复制到输出目录
  • 所有 CSV 文件的“复制到输出”属性设置为“始终”
  • 视觉工作室 2013
  • NHibernate 版本 4.0.4.4000
  • .NET 4.5.1
  • FluentNHibernate 版本 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(@"..\Dependencies\Oracle.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 = @".\UnitTests\ViewModelTests\FixedPrologueTestData.csv";
    private const string DATA_OUTPUT_PATH = @"UnitTests\ViewModelTests";
    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 驱动程序?

4

1 回答 1

0

我最终用编译器标志“修复”了这个问题:

    [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年多了,事情一直很稳定。

于 2021-01-20T12:37:21.633 回答