Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which tests to make for this little method?

I currently have the following method:

public void SetNewRandomValue() {
   double newValue = numberGenerator.GenerateDouble(
                             genesValuesInterval.MinimumValue,
                             genesValuesInterval.MaximumValue
                             );

   this.value = newValue;
}

What should be the guidelines for deciding how many tests (and which tests) to make to this method? I currently have done the following one (only after implementing the method -- that is, not test-first):

var interval = new Interval(-10, 10);
var numberGeneratorMock = new Mock<INumberGenerator>(MockBehavior.Strict);
var numberGenerator = numberGeneratorMock.Object;

double expectedValue = 5.0;

numberGeneratorMock.Setup(ng => 
        ng.GenerateDouble(interval.MinimumValue, interval.MaximumValue))
        .Returns(expectedValue);

var gene = new Gene(numberGenerator, 0, new Interval(-10, 10));
gene.SetNewRandomValue();

Assert.AreEqual<double>(expectedValue, gene.Value);

that basically just tests one situation. Regression-testingwise I'd say that I can't think of a way of messing up the code, turning it into mal functioning code and still have the test pass, that is, I think the method looks decently covered.

What are your opinions on this? How would you handle this little method?

Thanks

like image 474
devoured elysium Avatar asked Dec 03 '10 20:12

devoured elysium


2 Answers

I would examine the code coverage with whatever testing tool you use, if a code coverage is available for your testing framework.

I personally like to work with either Microsoft Testing Tool or NUnit Testing Framework. I can then right-click my tests project and Test with NCover (while using NUnit), which will run the tests and tell me the percentage of code covered for each of my objects and tests.

I say that when you'll be done checking the code coverage which would result of at least a 98% code coverage, your code is likely to be well tested.

like image 170
Will Marcouiller Avatar answered Oct 02 '22 09:10

Will Marcouiller


I'd recommend taking a look at Pex - it can really help generate the kind of unit tests you're looking for (i.e. figure out the different potential paths and results given a method and return value).

like image 23
Bob Palmer Avatar answered Oct 02 '22 09:10

Bob Palmer