That sounds great.   I'll have to work out how to set the include path
to use the unittest-support.h and link the c file.  But I think I can
manage that.

Am I correct that for new tests (particularly written in c++) I should
use gtest and not the glib based style tests?

On Mon, Jan 12, 2026 at 4:29 PM John Ralls <[email protected]> wrote:
>
>
>
> > On Jan 12, 2026, at 11:49 AM, Stefan Koch <[email protected]> 
> > wrote:
> >
> > In an effort to learn the gnucash code, I am looking at adding more
> > coverage in the test framework.
> >
> > I am working on full coverage of the libgnucash/engine/qofid.c file.
> > (It did not have full coverage, and looked like it depended on the
> > least other stuffl.  I think the gtest is one that y'all consider
> > appropriate for new tests.)
> >
> > I get extra output when testing the error handling.  In particular, in
> > the this test....
> >
> > TEST(QofIDTest, collection_add)
> > {
> >    auto col = qof_collection_new(QOF_ID_BOOK);
> >    auto book = qof_book_new();
> >    auto job = gncJobCreate(book);
> >    gncJobBeginEdit(job);
> >
> >    EXPECT_FALSE(qof_collection_add_entity(NULL, NULL));
> >    EXPECT_FALSE(qof_collection_add_entity(col, NULL));
> >    EXPECT_FALSE(qof_collection_add_entity(NULL, QOF_INSTANCE(book)));
> >
> >    EXPECT_TRUE(qof_collection_add_entity(col, QOF_INSTANCE(book)));
> >    EXPECT_FALSE(qof_collection_add_entity(col, QOF_INSTANCE(book)));
> >    EXPECT_FALSE(qof_collection_add_entity(col, QOF_INSTANCE(job)));
> >
> >    gncJobDestroy(job);
> >    qof_collection_destroy(col);
> > }
> >
> > I get extra output for the check jince I cannot add a job to a
> > collection of books.  When running I get:
> >
> > 53: Test command: /home/sk/gnucash/build-debug/bin/test-qofid
> > 53: Working Directory: /home/sk/gnucash/build-debug/libgnucash/engine/test
> > 53: Environment variables:
> > 53:  GNC_UNINSTALLED=YES
> > 53:  GNC_BUILDDIR=/home/sk/gnucash/build-debug
> > 53: Test timeout computed to be: 10000000
> > 53: Running main() from /usr/src/googletest/googletest/src/gtest_main.cc
> > 53: [==========] Running 2 tests from 1 test suite.
> > 53: [----------] Global test environment set-up.
> > 53: [----------] 2 tests from QofIDTest
> > 53: [ RUN      ] QofIDTest.collection_new
> > 53: [       OK ] QofIDTest.collection_new (0 ms)
> > 53: [ RUN      ] QofIDTest.collection_add
> > 53:
> > 53: (process:179319): gnc.engine-CRITICAL **: 14:35:03.788: gboolean
> > qof_collection_add_entity(QofCollection*, QofInstance*): assertion
> > 'coll->e_type == ent->e_type' failed
> > 53: [       OK ] QofIDTest.collection_add (0 ms)
> > 53: [----------] 2 tests from QofIDTest (0 ms total)
> > 53:
> > 53: [----------] Global test environment tear-down
> > 53: [==========] 2 tests from 1 test suite ran. (0 ms total)
> > 53: [  PASSED  ] 2 tests.
> > 1/1 Test #53: test-qofid .......................   Passed    0.02 sec
> >
> > This is due to the "g_return_val_if_fail (coll->e_type == ent->e_type,
> > FALSE);" inside the qof_collection_add_entity function.
> >
> > The test still passes, but the output is messed up.   Is there a way
> > to setup the expectation for this assertion failure, and thus hide the
> > output?  Or should I not worry about this?
>
> There is, but it’s part of Glib’s testing apparatus. The infrastructure for 
> it is in 
> https://github.com/Gnucash/gnucash/blob/stable/common/test-core/unittest-support.c
>  and you can see an example of its use in 
> https://github.com/Gnucash/gnucash/blob/1d9b80f7ff4f53c2e8cdaa79d2be3fe5e0625c95/libgnucash/engine/test/utest-Account.cpp#L470.
>  We set up some logging variables in the first 4 lines, then change the log 
> handlers in lined 482 and 483. The checked handlers count the number of times 
> a particular error is emitted and that’s tested in lines 485 and 487. The 
> default logger is restored in line 489 but the fatal handler isn’t, and 
> there’s one more check at line 492.
>
> I’ve never tried using it in a Googletest test suite, but I can’t think of 
> any reason it wouldn’t work. You’d just change the assert macro from 
> g_assert_cmpint to EXPECT_EQ.
>
> Regards,
> John Ralls
>
>
_______________________________________________
gnucash-devel mailing list
[email protected]
https://lists.gnucash.org/mailman/listinfo/gnucash-devel

Reply via email to