2011-12-26 26 views
4

我想在使用xUnit 1.8.0.1549的dll应用程序(VS2010/C#)中运行测试。 为此,我通过Visual Studio使用“Start External Program”在项目属性中的“Start Action”下运行xUnit,通过GUI runner(C:\ mypath \ xunit.gui.clr4.x86.exe)运行dll。Visual Studio与xUnit,Assert.Throws和“异常是由用户代码未处理”

我想测试一下,如果一些方法引发异常,要做到这一点,我使用类似以下内容:

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

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

回答

-1

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

我不知道任何方式来控制每个程序集的这种行为(只考虑xUnit是用户代码,而不是其他外部代码)。

0

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

这里有一个例子: 我有读取一个文件名,并进行一些处理方法:

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); 
     } 

现在您的测试应该通过!

相关问题