Visual studio with xUnit, Assert.Throws and "Exception was handled by user code"

I am trying to run tests inside a dll application (VS2010 / C #) using xUnit 1.8.0.1549. To do this, I run xUnit through the visual studio, using the "Start External Program" in the "Start Action" in the project properties, running the dll through the GUI runner (C: \ mypath \ xunit.gui.clr4.x86.exe).

I want to check if some methods throw an exception, for this I use something like the following:

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

The problem is that the debugger stops inside my method when an exception occurs, saying: "The exception was raw user code." This is bad, because he constantly stops the guy runner, forcing me to press F5. I would like to run tests smoothly, how do I do this? Thanks

+4
source share
3 answers

You can disable the break in exception behavior in VS. See http://msdn.microsoft.com/en-us/library/d14azbfh.aspx for inspiration.

0
source

When you check if an exception has occurred, you will have to handle the exception in the unit test code. Right now, you are not doing this.

Here is an example: I have a method that reads in the file name and does some processing:

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

... etc. Now I am writing a test method to test this code as follows:

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

Your test should pass now!

0
source

If you go to the Visual Studio settings and uncheck the box β€œOnly my code”, the xUnit infrastructure will be considered the user code, and those exceptions (which are expected by xUnit) will not ask you.

I do not know how to control this behavior on the assembly (just consider xUnit as user code, but not external code).

-1
source

Source: https://habr.com/ru/post/1388006/


All Articles