Exception in MS Unit Test?
Asked Answered
C

3

5

I created a unit test for a method of my project. That method raises an exception when a file is not found. I wrote a unit test for that, but I'm still not able to pass the test when the exception is raised.

Method is

public string[] GetBuildMachineNames(string path)
{
    string[] machineNames = null;

    XDocument doc = XDocument.Load(path);

    foreach (XElement child in doc.Root.Elements("buildMachines"))
    {
        int i = 0;
        XAttribute attribute = child.Attribute("machine");
        machineNames[i] = attribute.Value;
    }
    return machineNames;
}

Unit Test

[TestMethod]
[DeploymentItem("TestData\\BuildMachineNoNames.xml")]
[ExpectedException(typeof(FileNotFoundException),"Raise exception when file not found")]
public void VerifyBuildMachineNamesIfFileNotPresent()
{
    var configReaderNoFile = new ConfigReader();
    var names = configReaderNoFile.GetBuildMachineNames("BuildMachineNoNames.xml");
}

Should I handle the Exception in the method or am I missing something else??

EDIT:

The path I am passing is not the one to find the file, so this test should pass... i.e. what if file not exists in that path.

Crumple answered 4/2, 2011 at 10:23 Comment(2)
Whats the error message? Has a different exception been thrown? Or none at all?Karlynkarma
file not found!! actually I added the test data in Unit test project but not chaged the property "copy to output directory" as "always copy".... now its solveed. Thanks AnywayCrumple
M
7

In your unit test it seems that you are deploying an xml file: TestData\BuildMachineNoNames.xml which you are passing to the GetBuildMachineNames. So the file exists and you cannot expect a FileNotFoundException to be thrown. So maybe like this:

[TestMethod]
[ExpectedException(typeof(FileNotFoundException), "Raise exception when file not found")]
public void VerifyBuildMachineNamesIfFileNotPresent()
{
    var configReaderNoFile = new ConfigReader();
    var names = configReaderNoFile.GetBuildMachineNames("unexistent.xml");
}
Moreta answered 4/2, 2011 at 10:26 Comment(3)
No!! Even I am passing, but its giving wrong path, so other tests are also getting fail, becasue path not correct then this perticular test should pass, what i wrote for file nt found exception.Crumple
@Pawan, is FileNotFoundException actually thrown inside the GetBuildMachineNames method? If yes, then this test should pass. If no exception or another type of exception is thrown the test will fail.Moreta
Sorry, i am new in this area. Actually I was not running the unit test, i was debugging that.... Above code is correct. Jst one modification I made is in file property I set to "always copy"Crumple
R
0

By putting [ExpectedException(typeof(FileNotFoundException),"Raise exception when file not found")] attribute you are expecting that the method will throw an FileNotFoundException, if the FileNotFoundException not thrown Test will fail. Otherwise Test will be success.

Regulus answered 4/2, 2011 at 11:25 Comment(0)
L
0

I never really understood the point of ExpectedException. You should be able to catch exception in code rather than in attributes. It is a better practice and also allows you to do stuff after it is raised (e.g. more validations)... Also it would let you stop the code in debugger and check things out rather than need to ask in forums. :)

I'd use Assert.Throws( TestDelegate code );.
See here an example.

Liaison answered 4/2, 2011 at 22:34 Comment(1)
try catch the Exception should be in Main code, not in unit test. Example is on Nuint, m using MSunit test.Crumple

© 2022 - 2024 — McMap. All rights reserved.