|This tutorial needs a nhận xét. You can open a JIRA issue, or edit it in GitHub following these contribution guidelines. |
Bạn đang xem: Adding Unit Tests To A C Project
lltb3d.com IDE (including C/C++/Fortran support)
version 7.4 or 8.0 with lltb3d.com C/C+ plugin+
Java Development Kit (JDK)
Version 7 or 8
CUnit 2.1 kiểm tra framework
C Unit Testing Framework project on sourceforge.
See the lltb3d.com IDE Installation Instructions and + Configuring the lltb3d.com IDE for C/C+/Fortranfor information about downloading and installing the required lltb3d.com software.
lltb3d.com IDE with the C/C/Fortran plugin supports unit tests in C/C projects. You can use the IDE lớn create, run, debug, and view results of your C & C tests. This article shows you how to use unit tests in the IDE with your C/C projects.
A managed project is created with a logical folder called Test Files, where you can manage unit tests for the project. You can create tests directly under this Test Files folder or organize them in logical sub folders. A chạy thử node in the Test Files folder is a special folder with test-related actions. When a kiểm tra is built, it will exclude the main file from the project và otherwise use all other project settings.
The IDE supports only CUnit, CppUnit and its own "simple" output format, which means your tests should produce output of one of these types of tests. The IDE provides templates for unit tests:
This tutorial uses the CUnit kiểm tra framework and lltb3d.com C Simple Tests on Ubuntu Linux, but the techniques demonstrated can help you with the other types of tests on other platforms as well. The tutorial does not explain the CUnit or CppUnit testing frameworks. You need to lớn consult the documentation for those frameworks for information about how to use them.
To follow this tutorial, you must install CUnit on your system first. See more information about the CUnit framework at the C Unit Testing Framework project on sourceforge.
The CUnit documentation is included in the CUnit tải về, or you can view it at http://cunit.sourceforge.net/doc/index.html.
On Linux systems, you should be able to install libcunitl, libcunitl-doc and libcunitl-dev packages from your package repository.
On Mac OS, or on Linux if the libcunitl* packages are not available, you can tải về the CUnit source and build it as described here.
The instructions below assume you downloaded the tệp tin CUnit-2.1-2-src.tar.bz2 into the directory /tmp. If your tải về tệp tin has a different name be sure to lớn specify your actual CUnit download tệp tin and the actual directory where you saved it.
When the "make install" finishes, the CUnit chạy thử framework is ready to lớn use in the IDE and you can continue on khổng lồ Create the Project for the Tutorial.
You must build CUnit with the GNU build system before you can use CUnit tests. On Oracle Solaris 11 systems the GNU build system is not usually installed by mặc định. You can get the GNU build system components from the Oracle Solaris 11 package repository with the following commands:
pkilogam install pkg://solaris/developer/build/gnu-makepkg install pkg://solaris/developer/build/makepkg install pkg://solaris/developer/gcc-45pkilogam install pkg://solaris/system/headerpkg install pkg://solaris/developer/build/autoconfpkg install pkg://solaris/developer/build/automake-110
The instructions below assume you downloaded the tệp tin CUnit-2.1-2-src.tar.bz2 inlớn the directory /tmp. If your download file has a different name be sure lớn specify your actual CUnit download file and the actual directory where you saved it.
When the "make install" finishes, the CUnit thử nghiệm framework is ready lớn use in the IDE and you can continue on to lớn Create the Project for the Tutorial.
These instructions assume you downloaded the tệp tin CUnit-2.1-2-src.tar.bz2 inkhổng lồ the directory C:/distr. If your tải về file has a different name be sure lớn specify your actual CUnit tải về file và the actual directory where you saved it.
Start the MinGW shell application in Windows by choosing Start > All Programs > MinGW > MinGW Shell.
In the MinGW Shell window, unpack the CUnit-2.1-2-src.tar.bz2 file as follows:
C:UsersusernameAppDataLocalTemp on /tmp type web11_user (binmode,noumount)C:MinGWmsys1.0 on /usr type web11_user (binmode,noumount)C:MinGWmsys1.0 on / type web11_user (binmode,noumount)*C:MinGW on /mingw type web11_user (binmode)*
The last line in bold above sầu shows the Unix path is /mingw. Your system may report something different, so make a note of it because you need khổng lồ specify the path in the next comm&.
Configure the Maketệp tin with the following commvà.If your MinGW is not in /mingw, be sure to specify the appropriate Unix location of your MinGW with the --prefix= option.
$ ./configure --prefix=/mingw_(lots of output about checking và configuring)..._config.status: executing depfiles commandsconfig.status: executing libtool commands
$ makemake all-recursivemake<1>: Entering directory "c/distr/CUnit-2.1-2"Making all in CUnit..._(lots of other output)_make<1>: Leaving directory "c/distr/CUnit-2.1-2"$
Install the CUnit library into lớn C:/MinGW/include/CUnit, C:/MinGW/share/CUnit & C:/MinGW/doc/CUnit by running make install:
$ make installMaking install in CUnitmake<1>: Entering directory "c/distr/CUnit-2.1-2/CUnit"Making install in Sourcesmake<1>: Entering directory "c/distr/CUnit-2.1-2/Cunit/Sources"... _(lots of other output)_make<1>: Leaving directory "c/distr/CUnit-2.1-2"$
If you use Java 7 update 21, 25, or 40 you must perkhung the following workaround due khổng lồ issue 236867 in order lớn get CUnit và this tutorial lớn work.
Go lớn Tools > Options > C/C++ > Build Tools and select the MinGW tool collection.
Change the Make Comm& entry khổng lồ make.exe pháo without a complete path.
Exit the IDE.
On Windows 7 & above, type var in the Start menu’s search box to lớn quickly find a links khổng lồ Edit the system environment variables.
Select the Advanced tab and cliông chồng Environment Variables.
In the System Variables panel of the Environment Variables dialog, select cliông xã New.
Set the Variable Name to lớn MAKE and the Variable Value lớn make.exe.
Clichồng OK in each dialog khổng lồ save sầu the change.
Start the IDE & continue to the next section.
When the "make install" finishes, your CUnit is ready to lớn use in the IDE và you can continue on to Create the Project for the Tutorial.
In Cygwin you can install CUnit using the standard Cygwin installer, setup-x86.exe cộ or setup-x86_64.exe pháo, available from http://cygwin.com/install.html. The CUnit package is located in the "Libs" category và you can install it in the same way you install other packages.
Be sure to lớn use the correct version. You must use 64-bit Cygwin và CUnit if you are running 64-bit lltb3d.com IDE.
If you have not yet installed Cygwin, see the general Cygwin installation info in + Configuring lltb3d.com IDE for C/C+/Fortran. You can install CUnit by selecting it in the Libs category in the setup program.
Choose File > New Project.
In the project wizard, cliông chồng C/C và then select C/C Application.
In the New C/C++ Application dialog box, select Create Main file & select the C language. Accept the defaults for all other options.
In the Projects window, open the Source Files thư mục and double-cliông chồng the main.c tệp tin to open it in the editor. The file’s content is similar khổng lồ that shown here:
To give sầu the program something to bởi vì, replace the code in the main.c file with the following code lớn create a simple factorial calculator: