Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing with long inputs

[TestMethod]
public void SomeTestMethod()
{
    string input = "some looooong input...";

    var proc = new Processor()
    string result = proc.DoSomething(input);

    Assert.Equals("good", result);
}

If I'm writing a unit test and I have an input that is extremely long (such as EDI transactions), should I just paste that into my test method as a long string?

Others have suggested I should paste that long string into a file and treat that file as an embedded resource in my test project. If I do something like that and I need different inputs for each of my tests, I could see a lot of files piling up and becoming hard to maintain.

Are there any best practices surrounding this? Should I just continue pasting these long strings into my test methods?

like image 681
Books Avatar asked Jan 06 '12 16:01

Books


People also ask

How big should a unit test be?

One unit test case should test only one thing. Therefore, there should be only one set of AAA in one test case. A test case shouldn't be very long (longer than 10 lines of code) if it follows the AAA pattern.

What are the two types of unit testing?

There are 2 types of Unit Testing: Manual, and Automated.


2 Answers

You can use a different string constructor to create a very long string of repeated characters, such as this:

string input = new string('x', 1024 * 1024 / 2);

That approach gives a much more elegant way of creating long strings withing having to paste long strings into your tests.

like image 141
Shawn Avatar answered Oct 11 '22 00:10

Shawn


I always put long test strings into resources, and maintain consistent naming between tests and their resources to keep the mapping easy. I use the same name for the resource and the test. When I need several resources for a test, I add a suffix 1, 2, 3, and so on.

like image 38
Sergey Kalinichenko Avatar answered Oct 10 '22 23:10

Sergey Kalinichenko