Alpha75
Alpha75

Reputation: 2280

Asserts in helper methods

I'm implementing some test cases in Visual Studio (TestTools). I have to reuse a common logic in some of them. So I decided to extract this logic into a helper method. It is good practice to call Assert.AreEqual (..) from these helper methods? For example (edited):

[TestMethod]
public void myTest() 
{
    ...
    helperMethod();
    ...
}

private void helperMethod(int a)
{
    ...
    Assert.AreEqual("x", str);
    ...
    Assert.AreEqual("y", str2);
    ...
}

Upvotes: 4

Views: 2491

Answers (1)

P. Roe
P. Roe

Reputation: 2117

In my opinion the best practices for unit tests are still being debated so I'm sure you may get feedback from both sides regarding what the best practice is in this scenario and I am going to refrain from weighing in.

That being said have you considered returning the value from the helper method and then asserting in the primary unit test?

Additionally my personal preference is to label all helper methods as private/internal and/or placing them in a separate helper classes.

Lastly, I can see in your example that you are not passing any arguments to your helper method. That may be simply be a hastily written example but in case it's not let me point out a best practice that most people agree on. Never let a unit test method or helper method depend on class values. Stated differently, all unit test methods and their helpers should try their best to be pure.

Upvotes: 3

Related Questions