How do you create tests for “make check” with GNU autotools

心已入冬 提交于 2019-12-02 23:47:23

To make test run when you issue make check, you need to add them to the TESTS variable

Assuming you've already built the executable that runs the unit tests, you just add the name of the executable to the TESTS variable like this:

TESTS=my-test-executable

It should then be automatically run when you make check, and if the executable returns a non-zero value, it will report that as a test failure. If you have multiple unit test executables, just list them all in the TESTS variable:

TESTS=my-first-test my-second-test my-third-test

and they will all get run.

Dongho Yoo

I'm using Check 0.9.10

    configure.ac
    Makefile.am
    src/Makefile.am
    src/foo.c
    tests/check_foo.c
    tests/Makefile.am
  1. ./configure.ac

    PKG_CHECK_MODULES([CHECK], [check >= 0.9.10])

  2. ./tests/Makefile.am for test codes

    TESTS = check_foo
    check_PROGRAMS = check_foo
    check_foo_SOURCES = check_foo.c $(top_builddir)/src/foo.h
    check_foo_CFLAGS = @CHECK_CFLAGS@
    
  3. and write test code, ./tests/check_foo.c

    START_TEST (test_foo)
    {
        ck_assert( foo() == 0 );
        ck_assert_int_eq( foo(), 0);
    }
    END_TEST
    
    /// And there are some tcase_xxx codes to run this test
    

Using check you can use timeout and raise signal. it is very helpful.

You seem to be asking 2 questions in the first paragraph.

The first is about adding tests to the GNU autotools toolchain - but those tests, if I'm understanding you correctly, are for both validating that the environment necessary to build your application exists (dependent libraries and tools) as well as adapt the build to the environment (platform specific differences).

The second is about unit testing your C++ application and where to invoke those tests, you've proposed doing so from the autotools tool chain, presumably from the configure script. Doing that isn't conventional though - putting a 'test' target in your Makefile is a more conventional way of executing your test suite. The typical steps for building and installing an application with autotools (at least from a user's perspective, not from your, the developer, perspective) is to run the configure script, then run make, then optionally run make test and finally make install.

For the second issue, not wanting cppunit to be a dependency, why not just distribute it with your c++ application? Can you just put it right in what ever archive format you're using (be it tar.gz, tar.bz2 or .zip) along with your source code. I've used cppunit in the past and was happy with it, having used JUnit and other xUnit style frameworks.

You can use Automake's TESTS to run programs generated with check_PROGRAMS but this will assume that you are using a log driver and a compiler for the output. It is probably easier to still use check_PROGRAMS but to invoke the test suite using a local rule in the Makefile:

check_PROGRAMS=testsuite

testsuite_SOURCES=...
testsuite_CFLAGS=...
testsuite_LDADD=...

check-local:
    ./testsuite
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!