Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am newbie to TDD (writing first project following TDD practices). I have fairl

ID: 642130 • Letter: I

Question

I am newbie to TDD (writing first project following TDD practices).

I have fairly basic interface IProfiler and an implementation Profiler.

interface IProfiler
{
bool IsBusy {get;}
long Elapsed {get;}
}
A Simple Test

    IProfiler profiler = default(Profiler);
    [TestInitialize]
    public void Initialize()
    {
        profiler = new Profiler();
    }

    [TestMethod]
    public void ProfilerInitializationTest()
    {
        Assert.AreEqual(false, profiler.IsBusy);
        Assert.AreEqual(true, profiler.ElapsedMilliSeconds == default(long));
    }
The question is : As I will have more state, Number of asserts will increase. Should I keep separate Assert to test each field or should i include all here, or should I group into 3-4 similar asserts?

Explanation / Answer

Stick to one assertion per test, but not necessarily one Assert.

In my experience, the best way to get the right balance, is to name each method Should...

In your example, that would be ShouldInitializeWithCorrectDefaults(). That is a single assertion, regardless of how many Assert methods you call. As soon as I run into a situation where I'm struggling to not use "And" in my method name, I suspect I'm testing too much.

Also, try to keep in mind the question, "if I introduce a bug here, how many tests will break?" The ideal (but not always practical) answer is 1. I introduce a bug which stops it initializing correctly, the ShouldInitializeWithCorrectDefaults() test should break, because it should and now it doesn't.

Unfortunately, initialization is one of those examples where you'll probably break every test in the class. Which raises the question, why bother testing it by itself? Sometimes there are good reasons, but often you just go in and think, "Well, I'll know that if every test breaks, it's something common to every test, which is probably class instantiation. So does it need its own test?"