One of the unit test homework guidance series (2)

xiaoxiao2021-03-06  108

3. Add the following code to the launcher to run the Test Case Selection dialog:

#ifdef _debug // includes test header file #include #include static afx_extension_module extTestrunner; # Endif // The following is a test code, this part of the test does not appear In the published version #ifdef _debugteestrunner runner; runner.addtest (cppunit :: testfactoryregistry :: getRegistry (). Maketest (); runner.run (); # ENDIF

4, production distribution

The release needs to do the following:

Set the property of Project to Release (this will automatically remove the _debug declaration);

Remove the test file from the engineering project (ie, files with TEST);

3.1.2 Test Environment Use Visual C , Windows Non-Window Applications

3.1.2.1 For the Former Project: After using the CPPUnit1.6.2, after decompression, the path is x: //cppunit-1.6.2;

Configuring a test framework in an engineering document: Add to the path of the executive header x: //cppunit-1.6.2/include, add the path of the import library file x: //cppunit-1.6.2/lib;

Configure the Debug (Test) version environment:

Add a static test box module CPPUNITCD.LIB (test framework) that needs to be linked;

Enable RTTI in Project Settings / C / C Language;

3.1.2.2 Establish a test case:

1. Take the Test "name test unit file name, such as the" CMAbstring "class name, such as the" cmabstring "class, named MabString.cpp, the test unit file is named TestMabString.cpp;

2, join the test frame header file and the unit header to test, take TestMabString as an example:

Header file: TestmabString.h

3, test samples

3.2 C standard

3.2.1 Test Environment Using GCC, Linux Non-Window Application

Previous: Using Check0.8.0, after decompression, the path is /xx/check-0.8.0;

Configure the test framework usage environment (I recommend using AutoConf and Automake using AutoConf and Automake using AutoConf and Automake, because use them can build Configure scripts and makefile files that meet international standards, and can effectively create compression packages and convenient distribution Required documents (also convenient to remove test case files in the release):

l First, you need to write configure.in files, this file is used for AutoConf to generate configure executable scripts; the framework of Configure.in is approximately as follows:

DNL This file is used to generate configure scripts.

DNL AC_INIT's XXXX.H parameter represents this valid file name in this directory

AC_INIT (XXXX.H)

Two parameters of DNL AM_INIT_AUTOMAKE are the version and version number of the application, respectively.

DNL may have some versions of AutoConf and Automake do not support this macro

AM_INIT_AUTOMAKE (XXXX, X.x) DNL The following is the test of compilation dependencies

DNL CHECKS for Program.

AC_PROG_AWK

AC_PROG_CC

AC_Prog_Install

AC_PROG_LN_S

DNL CHECKS for Libraries.

Ac_check_lib (Check, Suite_create)

DNL CHECKS for Header Files.

Am_config_header (config.h)

DNL Checks for Typedefs, Structures, And Compiler Characteristics.

DNL Checks for Library Functions.

DNL outputs the Makefile.in file generated by Automake as a Makefile file

AC_OUTPUT (MAKEFILE)

(Tip: AutoScan can generate the basic framework of the configure.in file, but very basic, the basic complement of the configure.scan file generated, then renamed configure.in)

l Write the makefile.am file, used for Automake to generate the Makefile.in file, the approach framework for the makefile.am file is as follows: (where xxxx is the application file name, such as the test program file name of the Program.c file, I recommend being Check_Program.c. ;)

TESTS = check_xxxxnoinst_PROGRAMS = check_xxxxframe_path = xx / check-0.8.0xxxx_docs = / srcfilelist_1 / srcfilelist_2 / ....... / ..... xxxx_SOURCES = / srcfilelist_1 / srcfilelist_2 / ....... EXTRA_DIST = $ ( XXXX_DOCS) include = -i $ (frame_path) / src -i $ (other_path) / includeldaddddd = / full (Frame_path )/src/libcheck.acleanfiles=*.* ~

(Makefile.am has a lot of tags, you can refer to the corresponding document. But commonly used as: noinst_programs for the generated executable, xxxx_sources (Application Name Try _Sources) is a list of source files, extra_dist is not required for the publisher File list (with this method can be removed), including the header file to be included, CHECK's header file location in the SRC in its installation directory; ldadd is the library name of the library to be linked, libcheck.a is CHECK test framework file;)

Use Automake -a -Foreign to generate a makefile.in file, - Foreign is to generate several external files such as install.sh, etc., if these files can be omitted;

Use AutoConf to generate configure execution scripts; then execute ./configure to generate your makefile file;

Perform make to generate an executable;

3.2.2 Establishing test cases:

1. Take the "Check_" named test unit file name in the program file name, such as the test unit file of the Money.c file named Check_money.c;

2, join the test frame header file and the unit header to test, take Check_money as an example:

Header file: Money.h; Source File: Money.c; Test Unit File: Check_Money.c: The test file frame is as follows:

#include #include #include "Money.h" / * Establish the necessary test variable, Money is the structure defined in Money.h Struct Money * / Money * Five_dollars; / * unit test Initialization function * / void setup (void) {FIVE_DOLLARS = Money_create (5, "USD");} / * Unit test end function * / void teardown (void) {Money_Free (Five_dollars);} / * unit test case, use case name Test_create * // * Test functions: Money_amout () * / start_test (test_create) {/ * functional test, black box test * // * Normal test * / fail_unless (Money_Amount (Five_dollars) = 5, "Amount Not SET CORRECTLY On Creation "); Fail_Unless (Strcmp (Money_Currency (Five_dollars)," USD ") = 0," Currency Not Set Correctly On Creation "); / * Conditions and Error path test, belonging to white box test * // * EXTRA TEST * /} END_TEST / * Unit test case, use case TEST_NET_CREATE * / START_TEST (TEST_NEG_CREATE) {Money * m = Money_Create (-1, "USD"); fail_unless (m = = null, "Null Should Be Returned ON attempt to create with a negative amount ");} END_TEST / * test unit, use cases named test_net_create * / START_TEST (test_zero_create) {Money * m = money_create (0," USD "); fail_unless (money_amount (m) = = 0, "Zero IS A Valid Amount of Money");} END_TEST / * unit test assembly, put all unit test groups into a "box", "box" name Money * / suite * money_suite (void) {suite * s = suite_create ("Money"); / * Test case packet * / tcase * tc_core = tcase_create ("core"); tcase * tc_limits = tcase_create ("limits"); / * packet by adding "box" suite_add_tcase (s, tc_core); suite_add_tcase (s, tc_limits); / * respectively different use cases added packet * / tcase_add_test (tc_core, test_create); tcase_add_checked_fixture (tc_core, setup, teardown); / * This use case registration initialization and end function * // * The following use case will not register the initialization and end function * / tcase_add_test (TC_LIMITS, TEST_NEG_CREATE); Test_ZERO_CREATE); RETURN S;} / * Perform test case * / int main Void) {INT NF; Suite * s =

money_suite (); SRunner * sr = srunner_create (s); srunner_run_all (sr, CK_NORMAL); nf = srunner_ntests_failed (sr); srunner_free (sr); suite_free (s); return (nf == 0) EXIT_SUCCESS:? EXIT_FAILURE;} 3.2.3 Production release:

The production distribution is only necessary to configure another makefile.am, and the source file list in this file is added to the execution body, that is, the application contains the main function; can also join the release of the release in MakeFile.am for the test version. This can directly generate a beta program and a release program.

转载请注明原文地址:https://www.9cbs.com/read-102766.html

New Post(0)