I would like to write unit tests for some classes with methods having byte array arguments. There are about 100 methods in total, and the array size ranges from 5-10 to a few 100 bytes. How should I generate and store the test arrays?
I started to create them manually and store them in the test code in hex strings. I worked a lot with such binary strings, so I can read them relatively easily ("I don't even see the code. All I see is blonde, brunette, redhead.") The problem is, this approach is slow, and I think using an automatic generator would result in more maintainable tests. But how should I test that the output of the generator is correct? Sounds like Catch-22...
I'm assuming you want the bytes to actually represent deserializable objects, not just be random. A hundred bytes or so would make a small base64 encoded string. You could save your test inputs as base 64 encoded strings and then the test could grab the correct one and turn it into bytes:
const string someScenario =
"R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw==";
byte[] bytes = Convert.FromBase64String(someScenario);
You could figure out what the string would be in advance via your serializer, something like
public string SerializeAsBase64()
{
var session = new SessionCredentials { SessionKey = Guid.NewGuid() };
using (var mem = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(mem, session);
var bytes = mem.ToArray();
return Convert.ToBase64String(bytes);
}
}
To make sure it works do you have a deserializer also...? to serialize then deserialize then compare the originals are equal?
I suggest using Microsofts Text Templates as generator for your unit tests. Just add a .tt file to your project and implement the generator. The generation is done at design time (when you save or change your template) and results in a .cs file in your project. Your testcode can then be handled as normal 'handwritten' code with one test per byte array and method.
The generator can load definition files as well for your byte arrays and the expected output. But since it is done at design time your unit tests are not using the file system.
The whole T4 system is really worth a look: Design-Time Code generation by using T4 Text Templates
...and it is by default part of VisualStudio - no need to install anything or change your build scripts.
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