Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #35175

Re: C unit testing and regression testing

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c
Subject Re: C unit testing and regression testing
Date 2013-08-09 19:54 +1200
Message-ID <b6jli3FgltmU4@mid.individual.net> (permalink)
References <ku0i0g$e0v$1@dont-email.me> <slrnl07qc3.2m9.grahn+nntp@frailea.sa.invalid> <ku23uj$p3t$1@dont-email.me>

Show all headers | View raw


James Harris wrote:
> "Jorgen Grahn" <grahn+nntp@snipabacken.se> wrote in message
> news:slrnl07qc3.2m9.grahn+nntp@frailea.sa.invalid...
>> On Thu, 2013-08-08, James Harris wrote:
>>> What do you guys use for testing C code? From web searches there seem to
>>> be
>>> common ways for some languages but for C there doesn't seem to be any
>>> particular consensus.
>>>
>>> I am looking for an approach in preference to a package or a framework.
>>> Simple and adequate is better than comprehensive and complex.
>>
>> Here's what I use.
>>
>> 1. A script which goes through the symbols in an object file or archive,
>>    finds the C++ functions named testSomething() and generates code to
>>    call all of these and print "OK" or "FAIL" as needed.
>>
>> 2. A C++ header file which defines the exception which causes the
>>    "FAIL".  Also a bunch of template functions named assert_eq(a, b)
>>    etc for generating them.
>
> When such an assert_eq discovers a mismatch how informative is it about the
> cause? I see it has no parameter for descriptive text.

If it's anything like common unit test frameworks, it would output 
something like "failure in test whatever at line bla, got this, expected 
that" which is all you really need.

> That gives me an idea. I wonder if C's macros could be a boon here in that
> they could be supplied with any needed parameters to generate good testing
> code. Possibly something like the following
>
>    EXPECT(function(parms), return_type, expected_result, "text describing the
> test")
>    EXPECT(function(parms), return_type, expected_result, "text describing the
> test")

There is a common tool called "expect (http://expect.sourceforge.net ) 
which is frequently used for acceptance testing.

I use something similar with my unit test framework.  For example if I 
want to test write gets called with the expected file descriptor and 
data size, but don't care about the data I would write something like:

write::expect( 42, test::Ignore, size );

functionUnderTest();

CPPUNIT_ASSERT( write::called );

The harness maps mocked functions to objects, so they can have state and 
perform generic actions such as checking parameter values.

> Then a whole series of such EXPECT calls could carry out the simpler types
> of test. For any that fail the EXPECT call could state what was expected,
> what was received, and produce a relevant message identifying the test which
> failed such as
>
>    Expected 4, got 5: checking the number of zero elements in the array
>
> where the text at the end comes from the last macro argument.
>
> Of course, the test program could write the number of successes and failures
> at the end.

That's normal behaviour for a test harness.

>> 3. And then I write tests.  One function is one test case. No support
>>    for setup/teardown functions and stuff; the user will have to take
>>    care of that if needed.
>>
>> Doesn't really force the user to learn C++ ... and I suppose something
>> similar can be done in C.  Longjmp instead of exceptions?
>
> It's a good idea but I'm not sure that longjmp would be possible without
> modifying the code under test.

It's way easier with exceptions, another reason for using a C++ harness.

-- 
Ian Collins

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

C unit testing and regression testing "James Harris" <james.harris.1@gmail.com> - 2013-08-08 17:46 +0100
  Re: C unit testing and regression testing James Kuyper <jameskuyper@verizon.net> - 2013-08-08 13:29 -0400
    Re: C unit testing and regression testing Malcolm McLean <malcolm.mclean5@btinternet.com> - 2013-08-08 11:49 -0700
      Re: C unit testing and regression testing James Kuyper <jameskuyper@verizon.net> - 2013-08-08 16:33 -0400
        Re: C unit testing and regression testing Malcolm McLean <malcolm.mclean5@btinternet.com> - 2013-08-08 14:55 -0700
          Re: C unit testing and regression testing James Kuyper <jameskuyper@verizon.net> - 2013-08-08 18:35 -0400
            Re: C unit testing and regression testing "James Harris" <james.harris.1@gmail.com> - 2013-08-09 07:34 +0100
          Re: C unit testing and regression testing Ian Collins <ian-news@hotmail.com> - 2013-08-09 18:08 +1200
            Re: C unit testing and regression testing Malcolm McLean <malcolm.mclean5@btinternet.com> - 2013-08-09 03:16 -0700
      Re: C unit testing and regression testing Ian Collins <ian-news@hotmail.com> - 2013-08-09 18:04 +1200
    Re: C unit testing and regression testing "James Harris" <james.harris.1@gmail.com> - 2013-08-09 07:30 +0100
  Re: C unit testing and regression testing Les Cargill <lcargill99@comcast.com> - 2013-08-08 12:54 -0500
    Re: C unit testing and regression testing "James Harris" <james.harris.1@gmail.com> - 2013-08-09 07:37 +0100
      Re: C unit testing and regression testing Les Cargill <lcargill99@comcast.com> - 2013-08-09 07:46 -0500
  Re: C unit testing and regression testing Jorgen Grahn <grahn+nntp@snipabacken.se> - 2013-08-08 18:55 +0000
    Re: C unit testing and regression testing "James Harris" <james.harris.1@gmail.com> - 2013-08-09 07:58 +0100
      Re: C unit testing and regression testing Ian Collins <ian-news@hotmail.com> - 2013-08-09 19:54 +1200
        Re: C unit testing and regression testing "James Harris" <james.harris.1@gmail.com> - 2013-08-13 12:35 +0100
          Expect (was Re: C unit testing and regression testing) Jorgen Grahn <grahn+nntp@snipabacken.se> - 2013-08-13 19:10 +0000
      Re: C unit testing and regression testing Jorgen Grahn <grahn+nntp@snipabacken.se> - 2013-08-09 11:54 +0000
        Re: C unit testing and regression testing "James Harris" <james.harris.1@gmail.com> - 2013-08-13 12:45 +0100
          Re: C unit testing and regression testing Jorgen Grahn <grahn+nntp@snipabacken.se> - 2013-08-13 19:50 +0000
            Re: C unit testing and regression testing "James Harris" <james.harris.1@gmail.com> - 2013-08-14 10:14 +0100
              Re: C unit testing and regression testing Jorgen Grahn <grahn+nntp@snipabacken.se> - 2013-08-14 19:44 +0000
                Re: C unit testing and regression testing "James Harris" <james.harris.1@gmail.com> - 2013-08-15 13:10 +0100
                Re: C unit testing and regression testing Jorgen Grahn <grahn+nntp@snipabacken.se> - 2013-08-17 07:32 +0000
          Re: C unit testing and regression testing Ian Collins <ian-news@hotmail.com> - 2013-08-14 10:58 +1200
            Re: C unit testing and regression testing "James Harris" <james.harris.1@gmail.com> - 2013-08-14 10:25 +0100
              Re: C unit testing and regression testing Ian Collins <ian-news@hotmail.com> - 2013-08-14 21:53 +1200
  Re: C unit testing and regression testing Ian Collins <ian-news@hotmail.com> - 2013-08-09 07:17 +1200
    Re: C unit testing and regression testing Bart van Ingen Schenau <bart@ingen.ddns.info.invalid> - 2013-08-09 10:30 +0000
      Re: C unit testing and regression testing Ian Collins <ian-news@hotmail.com> - 2013-08-10 08:21 +1200
  Re: C unit testing and regression testing <william@wilbur.25thandClement.com> - 2013-08-08 13:37 -0700
    Re: C unit testing and regression testing Jorgen Grahn <grahn+nntp@snipabacken.se> - 2013-08-08 22:52 +0000
      Re: C unit testing and regression testing "James Harris" <james.harris.1@gmail.com> - 2013-08-09 08:17 +0100
        Re: C unit testing and regression testing Jorgen Grahn <grahn+nntp@snipabacken.se> - 2013-08-09 09:34 +0000
        Re: C unit testing and regression testing Les Cargill <lcargill99@comcast.com> - 2013-08-09 07:52 -0500
  Re: C unit testing and regression testing Siri Cruise <chine.bleu@yahoo.com> - 2013-08-09 05:29 -0700
  Re: C unit testing and regression testing Roberto Waltman <usenet@rwaltman.com> - 2013-08-11 20:19 -0400
    Test-driven development (was Re: C unit testing and regression testing) Jorgen Grahn <grahn+nntp@snipabacken.se> - 2013-08-17 07:17 +0000
      Re: Test-driven development (was Re: C unit testing and regression   testing) Ian Collins <ian-news@hotmail.com> - 2013-08-17 19:40 +1200
      Re: Test-driven development (was Re: C unit testing and regression testing) Les Cargill <lcargill99@comcast.com> - 2013-08-17 11:40 -0500

csiph-web