Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# library to automatically generate values for unit tests

I've read about a library that generates auto values for usage in unit test but could not find it. Basically, what I want was instead of:

[Test]
public void Test()
{
    int x = 2;
    int y = 5;

    Assert.AreEqual(7, ObjectUnderTest.Add(x, y));
}

I want to write:

[Test]
public void Test()
{
    int x = Lib.Int();
    int y = Lib.Int();

    Assert.AreEqual(x + y, ObjectUnderTest.Add(x, y));
}

Update:

AutoFixture is the one I'm looking for. With AutoFixture, my test will be:

[Test]
public void Test()
{
    var fixture = new Fixture();
    int x = fixture.CreateAnonymous<int>();
    int y = fixture.CreateAnonymous<int>();

    Assert.AreEqual(x + y, ObjectUnderTest.Add(x, y));
}
like image 694
tvbusy Avatar asked Jul 21 '11 10:07

tvbusy


3 Answers

I think you can use PEX!

Pex automatically produces a small test suite with high code coverage for a .NET program. To this end, Pex performs a systematic program analysis (using dynamic symbolic execution, similar to path-bounded model-checking) to determine test inputs for Parameterized Unit Tests. Pex learns the program behavior by monitoring execution traces. Pex uses a constraint solver to produce new test inputs which exercise different program behavior.

like image 103
danyolgiax Avatar answered Oct 15 '22 13:10

danyolgiax


AutoPoco lets you generate objects for tests.

like image 31
E.Z. Hart Avatar answered Oct 15 '22 15:10

E.Z. Hart


NBuilder is a great library for creating sets of test data and objects. It uses a fluent API so it's pretty easy, IMHO, to pick up and start working with it.

Like this (not compile tested):

[Test]
public void Test()
{
    int x = Builder<int>.CreateNew().Build();
    int y = Builder<int>.CreateNew().Build();

    Assert.AreEqual(x + y, ObjectUnderTest.Add(x, y));
}

There's ways to create random data as well.

like image 35
Yuck Avatar answered Oct 15 '22 13:10

Yuck