We often think that the code we write is okay and we don't need to test it. In the past, I thought that testing is a waste of time, so I didn't study it carefully.
Recently, I was eager to try unit testing. Combining my previous programming experience, I found that unit testing is at least one of the best ways to ensure software quality. A wave of programmers develop and maintain a product. There is a big difference between programmers, just like the difference between "no obvious error" and "no obvious error, how can we ensure the quality of products in continuous iteration, retain the correct part, and remove bugs? The architecture design focuses on interfaces, so unit testing can act as interfaces.
Through the unit test class, its behavior is in line with the original unit design goals. When writing a unit test, you can check the class behavior from multiple aspects to ensure that the class conforms to the design in such a scenario. In vistual Studio, the simplest unit test is to use its own functions (you do not need to find nunit Assembly from the Internet and directly reference "Microsoft. visual Studio. qualitytools. unittestframework "assembly is OK ). Another benefit is convenience of debugging. Unit test projects can break points under the tested class during testing, which saves debugging time.
I did this. The unit test code is put into an independent project, reference the project to be tested, and add the Assembly feature to the project to be tested as follows:
[assembly: InternalsVisibleTo("Projky.UnitTests")]
In this way, the unit test can be visible to the internal modifier class in the tested project without compromising the Assembly visibility.
A simple requirement is to convert hexadecimal strings such as "30d9132169211a45", "30-d9-13-21-69-21-1a-45", or "30 D9 13 21 69 21 1A 45" into byte [] arrays. A bytestring class is designed to meet the requirements.
internal class ByteString { public static Byte[] ConverterToBytes(string value) { if (value.IndexOf("-") > -1) { value = value.Replace("-", ""); } else if (value.IndexOf(" ") > -1) { value = value.Replace(" ", ""); } Debug.Assert(value.Length % 2 == 0, "Invalid byte string length."); List<Byte> list = new List<Byte>(value.Length / 2); for (int i = 0; i < value.Length; i += 2) { int bHi = GetInteger(value[i]); int bLow = GetInteger(value[i + 1]); Byte temp = (Byte)(bHi << 4 | bLow); list.Add(temp); } return list.ToArray(); } static int GetInteger(char ch) { if (Char.IsDigit(ch)) { return ch - ‘0‘; } int value = 10; switch (ch) { case ‘a‘: case ‘A‘: value = 10; break; case ‘b‘: case ‘B‘: value = 11; break; case ‘c‘: case ‘C‘: value = 12; break; case ‘d‘: case ‘D‘: value = 13; break; case ‘e‘: case ‘E‘: value = 14; break; case ‘f‘: case ‘F‘: value = 15; break; default: throw new NotSupportedException(ch.ToString() + " is not valid char."); } return value; } }
Then, you can compile the following test class to verify the behavior (Interface) of this class:
[TestClass] public class ByteStringTest { [TestMethod] public void ConverterToBytes() { string input = "30d9132169211a45"; Byte[] bytes = ByteString.ConverterToBytes(input); Assert.IsTrue(bytes.Length == input.Length / 2); Assert.IsTrue(bytes[0] == 0x30); Assert.IsTrue(bytes[1] == 0xd9); Assert.IsTrue(bytes[2] == 0x13); Assert.IsTrue(bytes[3] == 0x21); Assert.IsTrue(bytes[4] == 0x69); Assert.IsTrue(bytes[5] == 0x21); Assert.IsTrue(bytes[6] == 0x1a); Assert.IsTrue(bytes[7] == 0x45); input = "30-D9-13-21-69-21-1A-45"; bytes = ByteString.ConverterToBytes(input); Assert.IsTrue(bytes.Length == 8); Assert.IsTrue(bytes[0] == 0x30); Assert.IsTrue(bytes[1] == 0xd9); Assert.IsTrue(bytes[2] == 0x13); Assert.IsTrue(bytes[3] == 0x21); Assert.IsTrue(bytes[4] == 0x69); Assert.IsTrue(bytes[5] == 0x21); Assert.IsTrue(bytes[6] == 0x1a); Assert.IsTrue(bytes[7] == 0x45); input = "30 D9 13 21 69 21 1A 45"; bytes = ByteString.ConverterToBytes(input); Assert.IsTrue(bytes.Length == 8); Assert.IsTrue(bytes[0] == 0x30); Assert.IsTrue(bytes[1] == 0xd9); Assert.IsTrue(bytes[2] == 0x13); Assert.IsTrue(bytes[3] == 0x21); Assert.IsTrue(bytes[4] == 0x69); Assert.IsTrue(bytes[5] == 0x21); Assert.IsTrue(bytes[6] == 0x1a); Assert.IsTrue(bytes[7] == 0x45); } }
If the unit test fails, for example, in the assert. istrue () method, if the parameter is false, an exception is thrown in Vs, so that you can know where it is incorrect.
Note that the class carried by the unit test is marked with [testclass], which is public visibility, and the single independent test method in it is marked with [testmethod], which is also public visibility.
In Visual Studio, press Ctrl + R to run the unit test project automatically. If the tested class has a breakpoint, it will also be broken when the position is tested.