5

我正在尝试使用 xUnit 1.8.0.1549 在 dll 应用程序(VS2010/C#)中运行测试。为此,我使用项目属性中“启动操作”下的“启动外部程序”通过 Visual Studio 运行 xUnit,通过 GUI 运行程序 (C:\mypath\xunit.gui.clr4.x86.exe) 运行 dll。

我想测试某些方法是否引发异常,为此,我使用如下内容:

Assert.Throws<Exception>(
   delegate
   {
       //my method to test...
       string tmp = p.TotalPayload;
   }
);

问题是调试器在我的方法内停止,当引发异常时说“用户代码未处理异常”。这很糟糕,因为它一直停止 gui 运行器,迫使我按 F5。我想顺利运行测试,我该怎么做?谢谢

4

3 回答 3

0

您可以在 VS 中关闭异常行为的中断。请参阅http://msdn.microsoft.com/en-us/library/d14azbfh.aspx以获取灵感。

于 2012-01-14T22:16:50.763 回答
0

如果您进入 Visual Studio 选项并取消选中“仅我的代码”设置,xUnit 框架将被视为用户代码,并且那些异常(xUnit 期望的)不会提示您。

我不知道有什么方法可以控制每个程序集的这种行为(仅将 xUnit 视为用户代码,而不是其他外部代码)。

于 2016-02-19T22:59:43.623 回答
-1

当您检查是否发生异常时,您必须在单元测试代码中处理异常。现在,你没有这样做。

这是一个示例:我有一个读取文件名并进行一些处理的方法:

  public void ReadCurveFile(string curveFileName)
    {           
        if (curveFileName == null) //is null
            throw new ArgumentNullException(nameof(curveFileName)); 
        if (!File.Exists(curveFileName))//doesn't exists
            throw new ArgumentException("{0} Does'nt exists", curveFileName);     

...等现在我编写了一个测试方法来测试这个代码,如下所示:

    [Fact]
    public void TestReadCurveFile()
    {
        MyClass tbGenTest = new MyClass ();

        try
        {
            tbGenTest.ReadCurveFile(null);
        }
        catch (Exception ex)
        {
            Assert.True(ex is ArgumentNullException);
        }
        try
        {
            tbGenTest.ReadCurveFile(@"TestData\PCMTestFile2.csv");
        }
        catch (Exception ex)
        {
            Assert.True(ex is ArgumentException);
        }

现在你的测试应该通过了!

于 2016-06-13T20:58:31.400 回答