Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET and NUnit - TDD [closed]

I'm learning TDD with VB.NET and NUnit. I want to know what's the best thing to do: Use a lot of Assert methods inside of a test method or use a assert per method?

This is my code.

Imports NUnit.Framework

<TestFixture()> _
Public Class CalculatorTest
<Test()> _
Public Sub TestAdd()
    Dim calculator As Calculator = New Calculator()

    Assert.AreEqual(2, calculator.sum(1, 1))
    Assert.AreNotEqual(3, calculator.sum(2, 2))
    Assert.AreEqual(-1, calculator.sum(0, -1))
        Assert.AreNotEqual(3, calculator.sum(1, 1))
    End Sub
End Class
like image 535
Thomas Avatar asked Jun 28 '10 12:06

Thomas


People also ask

What is TDD in NUnit?

TDD stands for Test Driven Development, and it's a design process in software development. It relies on the repetition of a very short development cycle, and the requirements are turned into very specific test cases. There are a couple of steps in the TDD process: Write a unit test that fails.

How do I write a unit test case in NUnit?

To mark a method as a test case, we need to add a Test attribute. It tells the NUnit framework that it should run this method. It's a good approach to run our test methods with different arguments, without copy-pasting our test methods. We can define test method parameters by using the [TestCase] attribute.

How do I use Testcasesource in NUnit?

TestCaseSourceAttribute is used on a parameterized test method to identify the source from which the required arguments will be provided. The attribute additionally identifies the method as a test method. The data is kept separate from the test itself and may be used by multiple test methods.

What is are the feature's of NUnit testing?

NUnit provides a rich set of assertions as static methods of the Assert class. If an assertion fails, the method call does not return and an error is reported. If a test contains multiple assertions, any that follow the one that failed will not be executed.


1 Answers

A better way to think about it is to test one thing at a time. Use as many asserts as necessary to test that one thing, but typically only one. Multiple asserts can be a sign that you are testing more than one thing at a time but it's not, in my opinion, a hard and fast rule. The best guide is that you don't want to create dependencies in your tests between concepts that are independent.

In your example you are actually testing 4 things, though you actually probably only need two of them since they cover the same ground. I'd suggest testing what happens when you add two positive numbers, two negative numbers, and a negative and a positive with negative and positive results. Then I'd think about mathematical properties and test commutativity and the additive identity (zero). Finally, I'd test the boundary conditions -- positive and negative overflow, etc. Note, this may or may not be comprehensive, i.e., I think I've covered the bases, but I'm not trying too hard to be exhaustive; I just want to illustrate how you'd go about thinking about what tests to write and, yes, I'd make each of these separate tests with a single assert.

For something more complex, you may have more than one assert that tests the same "thing" -- e.g., you may want to check that a row is properly inserted in the DB with a given set of inputs. I think it's perfectly acceptable to test that all columns have their proper value in a single test, rather than test each property individually. Others may differ, but I don't think that in this case you are creating any dependencies by testing that all properties have their correct values because the insert is an atomic action.

like image 135
tvanfosson Avatar answered Nov 07 '22 09:11

tvanfosson