Should I use a seperate class per test?
- by user460667
Taking the following simple method, how would you suggest I write a unit test for it (I am using MSTest however concepts are similar in other tools).
public void MyMethod(MyObject myObj, bool validInput)
{
if(!validInput)
{
// Do nothing
}
else
{
// Update the object
myObj.CurrentDateTime = DateTime.Now;
myObj.Name = "Hello World";
}
}
If I try and follow the rule of one assert per test, my logic would be that I should have a Class Initialise method which executes the method and then individual tests which check each property on myobj.
public class void MyTest
{
MyObj myObj;
[TestInitialize]
public void MyTestInitialize()
{
this.myObj = new MyObj();
MyMethod(myObj, true);
}
[TestMethod]
public void IsValidName()
{
Assert.AreEqual("Hello World", this.myObj.Name);
}
[TestMethod]
public void IsDateNotNull()
{
Assert.IsNotNull(this.myObj.CurrentDateTime);
}
}
Where I am confused is around the TestInitialize. If I execute the method under TestInitialize, I would need seperate classes per variation of parameter inputs. Is this correct? This would leave me with a huge number of files in my project (unless I have multiple classes per file).
Thanks