[pacman-dev] [PATCH][RFC] Unit tests!

Andrew Gregory andrew.gregory.8 at gmail.com
Fri Apr 12 14:52:14 EDT 2013


Exposing and catching bugs deep in the internals of alpm or pacman can be
rather difficult with our current test suite, so I'd like to add a unit test
suite.

Here's the setup I've come up with so far:

* Tests use the check framework [1] (it's in the repos)
* Tests should include the source file (not the header) for whatever is being
  tested.  This is necessary because it seems like most of the low hanging
  fruit (ie functions that don't interact with the user or filesystem) are
  static.
* Test files must define a function "build_suite" that assembles their tests
  into a suite.
* Test files are built as dynamic libraries which are automatically
  discovered and loaded by the test runner.

Check organizes individual tests into suites and then cases and allows
individual suites/cases to be selected with an environment variable.  Right now
my example setup has exactly one case per suite because that's easier, I'm
thinking it would be better though to have one case per test so that individual
functions can be tested separately.  For instance, `CK_RUN_SUITE=util.c
./runner` would test everything in util.c, and
`CK_RUN_CASE=util.c/_alpm_strip_newline ./runner` would test only that one
function.

I've included a test runner and a couple stub tests with a minimal Makefile as
an example.  With check installed, running `make check` in test/alpm/unit
gives:

 $ make check
 ./runner
 Running suite(s): alpm
  alpm_list
  util.c
 75%: Checks: 4, Failures: 1, Errors: 0
 util_test.c:22:F:Core:check_alpm_strip_newline:0: Assertion 'output==expected'
 failed: output=="test", expected=="this will fail"
 make: *** [check] Error 1

[1] http://check.sourceforge.net/doc/check_html/index.html

Andrew Gregory (1):
  add unit tests

 test/alpm/unit/Makefile         | 16 +++++++++
 test/alpm/unit/alpm_list_test.c | 76 +++++++++++++++++++++++++++++++++++++++++
 test/alpm/unit/runner.c         | 47 +++++++++++++++++++++++++
 test/alpm/unit/util_test.c      | 34 ++++++++++++++++++
 4 files changed, 173 insertions(+)
 create mode 100644 test/alpm/unit/Makefile
 create mode 100644 test/alpm/unit/alpm_list_test.c
 create mode 100644 test/alpm/unit/runner.c
 create mode 100644 test/alpm/unit/util_test.c

--
1.8.2.1



More information about the pacman-dev mailing list