[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?
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.
There are 2 types of Unit Testing: Manual, and Automated.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With