C ++ practical skills (4)

Source: Internet
Author: User

It's easy to write complicated things. Due to functional requirements, Vczh Library ++ 3.0 is very outrageous to me. To develop and maintain traversal, reduce mistakes caused by carelessness, and enhance unit testing, regression testing, and testing tools, record some development tips for the benefit of others. You are welcome to join us.

The previous article mentioned some pointer and memory problems. Today I will talk about the unit test problems. If there are no requirements for the unit test framework in the team, we can use the simplest method to build the unit test framework that runs in the IDE, the entire framework only requires a dozen lines of code. Let's first consider what the unit test framework with the least functionality needs to accomplish. First, we need to run test cases one by one. Second, we need to check whether some conditions are true in a test case. For example, if we write a function to connect two strings, we generally need to perform the following test:
1 # include "MyUnitTestFramework. h" // wait and we will show you how to complete the header file content with the least code
2 # include ""
3
4 TEST_CASE (StringConcat)
5 {
6 TEST_ASSERT (concat ("a", "B") = "AB ");
7 TEST_ASSERT (concat ("a", "") = "");
8 TEST_ASSERT (concat ("", "B") = "B ");
9 TEST_ASSERT (concat ("", "") = "");
10.
11}
12
13 int wmain ()
14 {
15 return 0;
16}
If our unit test framework can be written in this way, it is much easier to do anything, and you do not need to register a lot of things like some other test frameworks, or write a lot of configuration functions. Of course, this time we only use the testing framework with the least functionality. In addition to running tests, this framework does not have other functions, such as which tests can be run, log something when an error occurs. The reason for running in IDE is that if false occurs in TEST_ASSERT, the row will crash immediately, and IDE will help you locate the wrong TEST_ASSERT, then show you all the context information, for example, callstack or something. You don't have to be sorry for the friendly tools. Why do you have to make unit tests so complicated? For all unit tests, you must run them all before submitting code.

Let's take a look at the unit test code above. First, the code in braces runs automatically when TEST_CASE is written. TEST_ASSERT crashes when the expression is false. Start with a simple one. How to Create a crash? The simplest way is to throw an exception:
1 # define TEST_ASSERT (e) do (if (! (E) throw "no food tonight. ";} While (0)
There are two points to note. First, we need to add parentheses. Otherwise, the inverse operator may make incorrect behaviors. For example, when e is a + B = c, if (! (A + B = c)..., without parentheses, it becomes if (! A + B = c)..., the meaning is completely changed. The second idea is that I used do {...} while (0) to enclose the statement. The advantage of doing so is that TEST_ASSERT (e) is like a statement at any time. For example, we may write:
1 if ()
2 TEST_ASSERT (x1 );
3 else if (B)
4 {
5 TEST_ASSERT (x2 );
6 TEST_ASSERT (x3 );
7}
If do {...} while (0) is not surrounded, this else will be bound to the if in the macro, and your code will be secretly modified.

Now TEST_CASE (x) {y} is left. What can be automatically run outside the main function? Anyone familiar with C ++ will know that it is the constructor of global variables. Therefore, y in the braces TEST_CASE (x) {y} can only be called in the constructor of the global variable. But we know that when writing a class, the braces of the constructor are completed, followed by the braces of the class, the name of the global variable, and the final semicolon. To remove this, it is clear that {y} should belong to a common function. How can we use this function as a global variable? The method is simple. Just declare the function first:
1 # define TEST_CASE (NAME)
2 extern void TESTCASE _ ## NAME ();
3 namespace vl_unittest_executors
4 {
5 class TESTCASE_RUNNER _ # NAME
6 {
7 public:
8 TESTCASE_RUNNER _ # NAME ()
9 {
10 TESTCASE _ ## NAME ();
11}
12} TESTCASE_RUNNER _ ## NAME ##_ INSTANCE;
13}
14 void TESTCASE _ ## NAME ()
Let's take a look at what code will be translated into TEST_CASE (x) {y:
1 extern void TESTCASE_x ();
2 namespace vl_unittest_executors
3 {
4 class TESTCASE_RUNNER_x
5 {
6 public:
7 TESTCASE_RUNNER_x ()
8 {
9

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.