Gains from a unit test training

Source: Internet
Author: User
Tags how to write test cases

Today, I attended a unit test training held internally in Corp. Several gains:

1. Applying Unit Test is pretty simple! I never thought that the idea to do unit test and its implementation is just that straightforward and easy! Remember the key point of unit test is the drive to do it by ourselves instead of using popular 3rd party test framework like mstest, Google test, nunit, etc. yes, the key point is: unit test is to test the simple unit of an application. from oo perspective, it shocould be a class in general thinking. then how to test a class without directly using a 3rd party tool? If really getting into that thinking, you will find the solution is that easy:

Basic considerations:

1. The key effort whatever method we use is to write the test cases to test the class.

2. As normal use, the test case shoshould be written in the way to use that class, like the class's user to use it.

3. Use asserts.

4. we need a way to collect up all unit test cases. ---- Yes, we need a test framework to support running test cases, which shoshould include collect/manage all test cases, generate simple report after running, handle exceptions to try to make whole test suite run completely, etc.

The solution:

1. How to assert? Wrap a macro like this_app_assert to do assertion.

2. we need to build up a basic test framework. it can be these classes: unittest, unittestmgr; and specific unittest can derive from unittest to provide its detail run code. and to support running test cases, we may need something called textcommand framework which is just like a doc CommandLine to allow write/run Text commands that are used to execute any work (run test cases work here ).

2. How to Write test cases for every testable class?

1. Wrap a macro like this_app_testcaseex (test case name) which can define the most part of a test case class.

2. To write a test case, it looks like below:

This_app_testcaseex (mytestcase1)

{

// Detail logic to test my class using this_app_assert.

}

2. gain some graceful ideas about software development. easy things wocould be easy and almost all specific implementation requirements (like visiting private data of a class in C ++ without exposing public interfaces to use metadata, just like C # reflection does) can easily find a way! Yes, "Easy thing is easy, while hard thing is possible "! There are already practical tips, design patterns, workarounds, solutions to solve a seemingly difficult issue you are facing. you need to expand the knowledge to get to know them, and figure out to use which to solve issues quickly!

3. There are famous unit test tools like mstest, Google test, nunit, etc. They provide better test framework while the core of unit test is just easy to be implemented by ourselves.

4. we are always facing plenty of code to read for some tasks. if with few documentation/comments, the straightforward thing right to do from US shocould be probably starting debugging and reading the related code base without prepared thinking as guidance. reading code can let you know the author's thinking, while thinking of a basic solution that is straightforward to finish the target task of that Section of code base will let you go further and understand quicker. in most cases, the solution if putting a little more thinking, shocould be very similar to the one in use. so do your thinking ahead of/in progress to read others code!

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.