您的位置:首页 > 大数据 > 人工智能

Gains from a Unit Test Training

2010-05-12 14:45 246 查看
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 should 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 should 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 should 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 sth 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 would 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 should 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, should be very similar to the one in use. So do your thinking ahead of / in progress to read others code!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: