i am trying to run tests inside a dll application (VS2010/C#) using using xUnit 1.8.0.1549.
To do so i run xUnit via visual studio using "Start External Program" under "Start Action" in the project properties, running the dll via the GUI runner (C:\mypath\xunit.gui.clr4.x86.exe).
I want to test if some methods raise exception, to do so, 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 the exception is raised saying "Exception was unhandled by user code". That's bad because it stops the gui runner all the time, forcing me to press F5.
I would like to run the tests smoothly, how do I do this?
Thanks
You can turn off the break on exception behavior in VS. See http://msdn.microsoft.com/en-us/library/d14azbfh.aspx for inspiration how.
When you check for whether the exception occurred, you will have to handle the exception in your unit test code. Right now, you aren't doing that.
Here's an example:
I have a method that reads in a 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 write a test method to test this code like this:
[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);
}
Now your test should pass !
If you go into the Visual Studio options and uncheck the "Just my code" setting, the xUnit framework will be considered user code and those exceptions (which xUnit expects) won't prompt you.
I don't know any way to control this behavior per assembly (only consider xUnit to be user code, but not other external code).