使用NUnit测试Windows 8商店应用程序

15

我目前正在为一门课程开发一个Windows Store应用程序(Windows 8),但在运行我的NUnit测试时遇到了问题。

我的解决方案/项目设置如下:

  • TheMetroApp.sln

    • SQLite-net.csproj - 类库(Windows Store应用程序)。文件从NuGet中获取。
    • DataModel.csproj - 类库(Windows Store应用程序)
    • UnitTests.csproj - 单元测试库(Windows Store应用程序)。NUnit框架从NuGet中获取。
    • TheMetroApp.csproj - 一个项目文件,从Windows SDK示例中获取。
  • 其他依赖项和工具

    • Windows 8 Pro RTM/Visual Studio 2012 RTM
    • ReSharper 7
    • NUnit 2.6.1
    • SQLite(按照此处的说明进行设置)

UnitTests 依赖于并引用 DataModel。DataModel 依赖于并引用 SQLite-net。我在 UnitTests 项目中添加的唯一内容是一个包含一些存根 NUnit 单元测试的单个类。据我所知,这些已经正确设置:

[TestFixture]
public class TaskSourceTests
{
    #region Private Class Members

    private ITaskSource _taskSource;
    private String _dbPath;

    #endregion

    #region Testing Infrastructure

    [SetUp]
    public void SetUp()
    {
        // This part makes NUnit/ReSharper have problems.
        _dbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "UnitTestDatabase.sqlite");
    }

    #endregion

    #region Misc. CRUD stuff

    [Test]
    public void CreateTaskTest()
    {
        // Save the task.
        Task task = new Task( "Some Task", "lol.", DateTime.Now, false );
        _taskSource.Save( task );

        // Confirm that it is in the task db.
        using( SQLiteConnection db = new SQLiteConnection( _dbPath ) )
        {
            const String query = "SELECT * FROM Task WHERE Id = ?";
            IList<Task> results = db.Query<Task>( query, task.Id );
            Assert.True( results.Contains( task ) );
        }
    }

    // ...and so on [but with stubs that are basically Assert.Fail( "" )].

    #endregion
}

这段内容是关于Windows 8 SDK样例项目TheMetroApp的介绍,其中加入了一些自定义的XAML表单。作者没有遇到任何问题。但他在使用单元测试运行器时遇到了问题。当他尝试使用官方的NUnit x86测试运行器(版本2.6.1)时,由于与证书相关的问题,测试失败 (详见此处)
UnitTests.TaskSourceTests.CreateTaskTest:
SetUp : System.InvalidOperationException : The process has no package identity. (Exception from HRESULT: 0x80073D54)

很遗憾,ReSharper的NUnit测试运行器失败了,原因完全相同。不幸的是,目前似乎没有解决办法。我还尝试过使用Visual Studio 2012中内置的测试运行器(通过NUnit Visual Studio Test Adapter)。当我尝试使用“运行全部”来运行我的测试时,我得到了以下输出:
------ Run test started ------
Updating the layout...

Checking whether required frameworks are installed...

Registering the application to run from layout...

Deployment complete. Full package name: "GibberishAndStuff"

No test is available in C:\Projects\project-name\ProjectName\UnitTests\bin\Debug\UnitTests.dll. Make sure that installed test discoverers & executors, platform & framework version settings are appropriate and try again.
========== Run test finished: 0 run (0:00:09.4873768) ==========

我注意到的一件奇怪的事情是,如果我在“测试资源管理器”中选择特定的测试并告诉它运行,我会得到一个略微不同的错误信息:

Could not find test executor with URI 'executor://nunittestexecutor/'.  Make sure that the test executor is installed and supports .net runtime version 4.0.30319.18010.

这有点令人困惑,因为我已经安装了NUnit测试适配器。在测试适配器的launchpad页面上没有看到类似的问题。
我不太确定接下来该怎么做。如果这行不通,我也不介意重新设计我的项目以使用xUnit.net,微软的单元测试框架或其他东西。如果我能让NUnit工作起来,那将是非常棒的。
谢谢!

这是一个好问题。我认为原因在于反射模型与.NET Core和经典.NET不兼容。同时也不确定是否可以将.NET Core应用程序加载到Windows桌面模式中。本文解释了其中的差异:http://blogs.msdn.com/b/dotnet/archive/2012/08/28/evolving-the-reflection-api.aspx - Robert
2个回答

2
我有一个Windows 7手机应用程序,也遇到了您所遇到的问题。我的解决方案是创建一个单独的“链接”项目,使用标准的.net库编译代码。链接项目将不会与单元测试/NUnit产生任何问题。
请参阅以下内容以获取更多信息:

http://msdn.microsoft.com/en-us/library/ff921109(v=pandp.40).aspx

http://visualstudiogallery.msdn.microsoft.com/5e730577-d11c-4f2e-8e2b-cbb87f76c044/

我已将应用程序移植到Windows 8,运行测试用例时没有任何问题。

0

我在为通用应用程序(W81 + WP81)创建单元测试时遇到了相同的错误消息。唯一的解决方案是停止使用 NUnit 并仅使用 MSTest。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接