Let's assume I wrote an extension method in C# for byte
arrays which encodes them into hex strings, as follows:
public static class Extensions
{
public static string ToHex(this byte[] binary)
{
const string chars = "0123456789abcdef";
var resultBuilder = new StringBuilder();
foreach(var b in binary)
{
resultBuilder.Append(chars[(b >> 4) & 0xf]).Append(chars[b & 0xf]);
}
return resultBuilder.ToString();
}
}
I could test the method above using NUnit as follows:
[Test]
public void TestToHex_Works()
{
var bytes = new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
Assert.AreEqual("0123456789abcdef", bytes.ToHex());
}
If I use the Extensions.ToHex
inside my project, let's assume in Foo.Do
method as follows:
public class Foo
{
public bool Do(byte[] payload)
{
var data = "ES=" + payload.ToHex() + "ff";
// ...
return data.Length > 5;
}
// ...
}
Then all tests of Foo.Do
will depend on the success of TestToHex_Works
.
Using free functions in C++ the outcome will be the same: tests that test methods that use free functions will depend on the success of free function tests.
How can I handle such situations? Can I somehow resolve these test dependencies? Is there a better way to test the code snippets above?
Then all tests of Foo.Do will depend on the success of TestToHex_works
-- So? You don't have classes that depend on the success of other classes?toHex
(or swap implementations). Apart from that everything is fine. Your code converting to hex is tested, now there’s another code using that tested code as a utility to achieve its own goal.