A preliminary study of VS2015 C + + program unit testing-errors and milestones from 0 to 1

Source: Internet
Author: User
Tags assert

Implementation process

At the beginning of the unit test this thing feel very afraid, after watching the Snow Sunny blog, feel that they can try to learn, found a blog, address:

VS2015 installation with C + + for simple unit testing

Before the establishment and initialization are relatively easy, but soon encountered a problem, the example is the interface of a class test, whether the non-class interface (such as the test of the function), asked the snow after the clear, it should be possible, then try it yourself.

Prepare to try the test void Word_to_word (word_count* word, wprd_count* word1) with the following code:

classword_count{//Word Counter class Public:    //friend struct std::hash<word_count>; Public:    //string* temp;//used to save real words.    string* STR;//the name of the word    string* NUM;//Numeric suffix    intNum_rear;//start bit of the numeric suffix    intStr_count;//number of words    intFlag//determine if it is within the high frequency    intSize//Word    string* Word;//Word partWord_count*next_ptr;    Word_count (); ~word_count ();};voidWord_to_word (word_count* Word, word_count*word1) {    //assign all the values of Word to word1* (WORD1-&GT;STR) = * (word->str); * (Word1->num) = * (word->num); Word1->num_rear=word->num_rear; //start bit of the numeric suffixWord1->str_count = word->Str_count; //number of wordsWord1->flag = word->Flag; Word1->size = word->size; * (Word1->word) = * (word->word); Word1->next_ptr=word->next_ptr;

But soon there was trouble, and the function was to assign string and other values (mostly int types) in Object Word to the Word1 class. (Here you think about the reason why writing this function is because the understanding of OOP is not home, clearly defined an object, why do you want to do this complex memory dump operation ... Define a new one directly. No, the pointer is just doing this. This is tantamount to reading the words in the process of my own crazy create countless copies, and they all can live to the end, in order to verify my guess, wait I want to try to change the code, see if this is the case).

OK, back to the original question, this is the middle of the operation, but the blog only refers to the type of int comparison, so I am not sure how to use the Assert, then to explore.

Then I thought, the string class overloaded the operator = =, that will not be able to directly areequal the internal string, the code is as follows:

Next is linked to the obj file, the link is complete, but the program error, errors are as follows:

Microsoft::visualstudio::testtools::unittesting::assert::areequal ": None of the 15 overloads can convert all parameter types

Looking into the code, it is found that areequal cannot judge whether the string class is equal, continue to look up the data and discover the Stringassert class, but after reading it, the Stringassert class is mostly judged by the substring and the include/start end, and no suitable method is found. Then looking back to the overloaded type of areequal, found that the appropriate overload parameters should be:

Since all uppercase letters are in this example, you can use True to

But he continued to error, the explanation is not right, then I found the string^ type of string, said to be a managed type, what is the meaning of the feeling to collapse. Regarding the meaning of this type of hosting, I feel it is necessary to devote time to learn ... I don't know if the teachers and classmates I see can teach me.

Finally I gave up the use of assert, decided to include <assert.h>, and then use the Assert (WORD1==WORD2) way to judge.

The final code is as follows:

voidTestMethod1 () {Word_count* h1=NewWord_count (); Word_count* h2=NewWord_count (); H1->flag = A; * (H1-&GT;STR) ="strtest123"; * (h1->num) ="123"; * (H1->word) ="strtest"; H1->num_rear =7; H1->size =Ten; H1->str_count =1; H1->next_ptr =NULL;            Word_to_word (H1, H2); Assert::areequal (H2->flag,h1->flag); Assert::areequal (H2->num_rear, h1->num_rear); ASSERT (* (h1->word) = = * (h2->word)); ASSERT (* (H1-&GT;STR) = = * (h2->str)); ASSERT (* (h1->num) = = * (h2->num)); ASSERT (* (h1->word) = = * (h2->word)); Assert::areequal (H2->str_count, h1->str_count); Assert::areequal (H2->size, h1->size); /*assert::areequal (* (H1->STR), * (H2->STR)); Assert::areequal (* (H1->num), * (H2->num)); */            //assert::areequal (* (H1->word), * (H2->word)); //                    //TODO: Add test logic here//};

Then I started the link, the specific operation of the tutorial has. But what I didn't think of was that since my project was a precompiled header, I only had the link at first. /consoleapplication4/debug/word_count.obj, do not know to link: /Consoleapplication4/debug/stdafx.obj, so the following error has been reported:

First of all, it turns out that the next one hours of more painful experience are due to my lack of attention to the warning, thinking that the problem is not big, so has been a crash ....

Here we first have to resolve the error. On this non-connected precompiled object, I started by saying that I should recompile the original project, but this method does not have any eggs. After I went to ask the snow sunny, Snow said she was all the directory under the *.obj are added to the additional dependencies, so I also decided to use *.obj to replace the word_count.h, (I also found that, in fact, to join the stdafx.h should be no problem).

Do this step error is not, the test program compiled through but the error ensued, the test program produced an exception. But happily, at least the test program started running, right.

Then handle the exception as follows:

The internet to see the debug_heap.cpp, found that does not solve the problem, and then query the _CrtIsValidHeapPointer (block), it may be a memory leak and so on errors, which makes me doubt the test program I wrote, So in the test code in just a few lines of random changes ... But in vain for half an hour, I finally decided to start with warning.

i first re-searched the Ignore/editandcontinue, but did not find the clue. Later, inadvertently, I saw a blog that specifically mentions the need to ignore the following warning in the two lib,msvcrt and UCRTB, however, when I generated the test, I found out where the IDE seems to have said to ignore the two libraries, This led me to think that the system has been automatically resolved (after all, it is warning), so I wasted 15 minutes on suspicion of themselves and procedures, and finally I deliberately put these two words online Baidu, found a blog in the narrative of a puzzling (because I do not understand) method, Just right-click the Testproject1à property àc/c++à generate code à run the library to another (MDÀMT), see figure:

So I tried a bit, but failed, so I took four options to try again, finally in the state of MDD, the test continues! And, finally, it shows the test explorer, which found that the test was successful! The following example of a failed test and a successful sample (on failure, success) can see that the reason for the failure is that the assert () is set to! =, but should actually be equal to.

At this point, this shows that my first unit test attempt has been completed! Although the test is a trivial function, but at least the completion of the transition from 0 to 1, exciting!

Feelings

It turns out that the first time with one thing the most painful is the error you do not know why he is, where to check, even he does not know what to do, so from 0 to 1 really need the greatest energy and patience.

First of all, I've been wondering if there's a problem with the test code, or even at the end of the question whether it's my link process or not, but there's no source for these doubts, so it's unreliable. But I did not pay attention to the warning, warning query although not without. But it is a cursory look at the conclusion is not (should give yourself a slap), after really must have to clean up the habit of warning AH!

Some useful information is attached below:

VS2015 Considerations for testing local C + + code using Microsoft's own Unit test framework

If the test class uses a precompiled header file, add stdafx.obj to the project settings

When you test the STL container, an exception error dialog box pops up. The solution is to ignore MSVCRT.lib and UCRT.lib

Assert class:

Unit test Assert class, unit Test assert class

Usage of assertion (assert)

assert.areequal Method:

Https://msdn.microsoft.com/zh-cn/library/microsoft.visualstudio.testtools.unittesting.assert.areequal (vs.80). aspx

Stringassert Method:

Https://msdn.microsoft.com/zh-cn/library/microsoft.visualstudio.testtools.unittesting.stringassert.aspx

Other Tutorials:

Visaul Studio2015 installation and how to use C + + unit Testing take you to the visual Studio --Unit test

A question about unit tests that are not resolved:

C + + unit Test one: not as simple as it seems--a few very practical questions

A preliminary study of VS2015 C + + program unit testing-errors and milestones from 0 to 1

Related Article

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.