A unit test framework with less than rows: lazytest

Source: Internet
Author: User

WhenAlgorithmOr when writing a tool class, we always need to write some tests.CodeBut if you write them all in the main function, it is inevitable that the Organization is chaotic and difficult to check; if you choose cppunit or gtest and other powerful unit testing frameworks, it is too heavy to kill the chicken-it is not convenient. Another option is Tut, template unit test framework. Unlike the previous two, tut uses C ++ template functions instead of macros, I think it is quite rich when I try it.

In fact, I only need a very simple framework, but only for an algorithm implementation or a tool class write test, rather than at the project level. For example, if I write a max function to calculate the two numbers, the test code can be written as follows:

 
Testcase (test_max_int) {assert_true (max (1, 10) = 10); assert_true (max (100, 10) = 100); assert_true (max (10, 10) = 10); Return true;} testcase (test_max_float) {assert_true (max (1.1, 10.1) = 10.1); assert_true (max (100.1, 10.1) = 100.1 ); assert_true (max (10.1, 10.1) = 10.1); Return true ;}

 

Then run_all_cases is enough.

If you think about it carefully, it is not difficult to implement it. We mainly consider the following aspects:

    • Automatic Registration of test case
      This can be implemented using a global static variable constructor when declaring testcase.
    • Test Case Management and Operation
      You just need to register all the cases in a container, and finally traverse the container to call the case.
    • Declaring case failure and increasing error information
      Use a macro to check an expression. If the expression fails, perform the following two tasks: one is the output error line and the other is the expression; the other is to return false to indicate the case failure.
The following is the implementation, You can also download the file: http://code.google.com/p/baiyanhuang/source/browse/trunk/LazyLib/LazyTest.h
/// Description: // a simple unit-test framework which aims to testing simple programs like Utility Class, algorithm... /// how to use: // you only need to know 3 macros to use this framework: testcase, assert_true, run_all_cases // testcase (testname) // {// assert_true (1 + 1 = 2); // return true ;//}//... // run_all_cases (); // Author: lzprgmr // Date: 1/8/2011 // # pragma once # include <map> # include <I Ostream> # If defined (_ Win32) # include <windows. h> # endif # If! Defined (lazytestout) # define lazytestout STD: cout # endif // typedefstypedef unsigned int uint32_t; typedef bool (* testfunc) (); typedef STD: Map <char *, testfunc> testcasemap; // manage and run all test casesclass testmgr {public: static testmgr * Get () {static testmgr _ instance; Return & _ instance ;} void addtest (char * tcname, testfunc tcfunc) {m_tclist [tcname] = tcfunc;} uint32_t runallcases () {uint32_t failure = 0; For (testcasemap: iterator it = m_tclist.begin (); it! = M_tclist.end (); ++ it) {lazytestout <"running" <it-> first <"... "<STD: Endl; bool Bres = runcase (IT-> second); If (Bres) lazytestout <" \ tpass "<STD: Endl; else failure ++;} lazytestout <"\ n" <"totally" <failure <"cases failed !!! "<STD: Endl; return failure;} PRIVATE: bool runcase (testfunc TF) {bool Bres = false; # If defined (_ Win32) // Windows use seh to handle machine exceptions _ Try {Bres = TF () ;}__ random t (exception_execute_handler) {lazytestout <"\ texception caught! "<STD: Endl; Bres = false;} # else // non-Windows OS that doesn't support Seh-The singal Mechanic (SIGSEGV) can't work well as seh to handle the problem Bres = TF (); # endifreturn Bres;} PRIVATE: testcasemap m_tclist;}; // register a test caseclass testcaseregister {public: testcaseregister (char * tcname, testfunc tcfunc) {testmgr: Get ()-> addtest (tcname, tcfunc) ;}; // to use this test framework, you on Ly need to know 3 macros: # define testcase (TC) \ bool Tc (); \ testcaseregister register _ # Tc (# TC, Tc); \ bool Tc () # define assert_true (expr) do {If (! (Expr) {\ lazytestout <"\ tfailed at:" <_ file _ <": Line" <__line _ <STD :: endl; \ lazytestout <"\ texpression:" <# expr <STD: Endl; \ return false ;}} while (false) # define run_all_cases () do {testmgr: Get ()-> runallcases ();} while (false)

 

If I run the following code:

 
# Include "../lazylib/lazytest. H" testcase (test1) {assert_true (1 + 1 = 2);} testcase (Test2) {assert_true (1 + 1! = 2);} testcase (test3) {# If defined (_ Win32) int * P = NULL; * P = 10; # endif assert_true (1 + 1> 2 );} int main () {run_all_cases (); Return 0 ;}

 

The output result is as follows:

 
Running test1. .. passrunning test2. .. failed at: C: \ source \ Alibaba yanhuang \ algorithm \ test. cpp: line 12 expression: 1 + 1! = 2 running test3.. Exception caught! Totally 2 cases failed !!!

 

Note the following points:

    • This code can be run on Mac or windows. It has never been tried in Linux. However, only hardware errors such as memory access errors are handled using seh in Windows. The singal mechanism in MAC cannot handle SIGSEGV as well as Seh.
    • When writing a case, the case name cannot be repeated (nonsense ?), In addition, true must be returned at the end of each case. This may simplify the process and I have never thought of how to do it ~~~
    • The information is output to STD: Out by default. You can also define your lazytestout before include the file.

Update:

For the problem that true must be returned at the end of each case, a static class can be used here, mainly to maintain the status with a static member, and the case compiled by a function with a returned value needs to be modified with two macro definitions:

 

// To use this test framework, you only need to know 3 macros: # define testcase (TC) \ class _ # TC \ {\ public: \ static bool Tc () \{\_ result = true; \ Run (); \ return _ result ;\}\ static void run (); \ private: \ static bool _ result ;\}; \ bool class _ # TC: _ result = true; \ testcaseregister register _ # Tc (# TC, class _ # TC: TC ); \ void class _ # TC: Run () # define assert_true (expr) do {If (! (Expr) {\ lazytestout <"\ tfailed at:" <_ file _ <": Line" <__line _ <STD :: endl; \ lazytestout <"\ texpression:" <# expr <STD: Endl; \ _ result = false; return ;}} while (false)

 

 

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.