|
Boost Users : |
Subject: Re: [Boost-users] Boost.Test BOOST_TEST/BOOST_REQUIRE_MESSAGE message arg problems
From: Olaf Peter (ope-devel_at_[hidden])
Date: 2018-08-18 18:38:33
>> OK, my test_observer_fixture with overloads is ready:
>>
>> class test_observer_fixture
>> {
>> Â Â Â Â struct test_observer : public utf::test_observer
>> Â Â Â Â {
>> Â Â Â Â Â Â Â virtual void test_unit_aborted(utf::test_unit const& tu)
>> override;
>> Â Â Â Â Â Â Â virtual void assertion_result(utf::assertion_result result)
>> override;
>> Â Â Â Â Â Â Â // are the others required for my use case?
>> Â Â Â Â };
>>
>>     test_observer                           observer;
>>
>> public:
>> Â Â Â Â test_observer_fixture();
>> Â Â Â Â ~test_observer_fixture();
>>
>> public:
>> Â Â Â Â void setup();
>> Â Â Â Â void teardown();
>> };
>>
>> and get called by my BOOST_DATA_TEST_CASE. What is the prefered way
>> to use this Fixture to save the test result in case of failure/abort?
>>
>>
>> BOOST_DATA_TEST_CASE( wave,
>> Â Â Â Â utf_data::make_delayed<testsuite::dataset_loader>(
>> "test_case/wave" ),
>> Â Â Â Â input, expected, orig_file_name)
>> {
>> Â Â Â ...
>> Â Â Â Â auto [parse_ok, parse_result] = parse(input, parser,
>> test_case_name);
>>
>> Â Â Â Â BOOST_REQUIRE(parse_ok);
>>
>> Â Â Â Â BOOST_TEST(parse_result == expected, btt::per_element());
>>
>> Â Â Â Â Â Â Â ??? my_diagnostic_saving .... (orig_file_name, input,
>> parse_result);
>> Â Â Â Â );
>> }
>>
>> Thanks,
>> Olaf
>
> Sorry for the late answer,
>
> In the teardown, you may check if the current test had failures. You
> can do so by accessing the global singleton "results_collector". If
> "tc" is the current test case (that you can get with
> "current_test_case"):
>
> test_results const& tr = results_collector.results( tc.p_id );
>
> will give you the result.
>
> If the fixture should be applied to each of the test case inside your
> suite, you can install it like this:
>
> https://www.boost.org/doc/libs/1_67_0/libs/test/doc/html/boost_test/tests_organization/fixtures/case.html#boost_test.tests_organization.fixtures.case.fixture_for_a_complete_subtree
>
>
> Accessing via the test result will let you check if this is a failure
> or an abort: the BOOST_TEST_REQUIRE (BOOST_REQUIRE is deprecated) will
> fire an abort if I remember well. Using this way to install the
> fixture will also let you access the instance of the fixture directly
> (see examples in the documentation).
>
Since my global testobserver works for all events which may interested
even in the future (e.g exceptions), I have problems to apply them in my
use case.
I've added a member function to my testobserer fixture
void test_obserer_fixture::failure_diagnostic(std::string const&, ...)Â
which I can activate on failed tests using the events I've got from
utf::observer::assertion_result()
Now, in file test_main.cpp I have:
-----------------------------------------
#define BOOST_TEST_MODULE "Test Suite"
#include <boost/test/included/unit_test.hpp>
#include <testsuite/test_observer_fixture.hpp>
using test_observer_fixture = testsuite::test_observer_fixture;
BOOST_TEST_GLOBAL_FIXTURE(test_observer_fixture);
-----------------------------------------
and in (I have several)Â concrete test case file:
------------------------------------------
#include <boost/test/unit_test.hpp>
#include <boost/test/output_test_stream.hpp>
#include <testsuite/test_observer_fixture.hpp>
...
BOOST_AUTO_TEST_SUITE( my_testsuite )
BOOST_DATA_TEST_CASE( failure_test,...
   ...,
   input, expected, test_case_name)
{
...
}
-------------------------------------------
and I can see/log:
# assertion failed: failure_test/test_case/_0
But how to access the global fixture now?
I've changed the test case to:
------------------------------------------
...
BOOST_DATA_TEST_CASE_F(test_observer_fixture, failure_test,
   ...,
   input, expected, test_case_name)
{
   ...
   failure_diagnostic(...);
}
-------------------------------------------
Boost.Test ships a BOOST_DATA_TEST_CASE_F() macro for this case, which
is a 'local' fixture, each time constructed and destroyed which is
expensive - so I like the idea of a global fixture to save to
filesystem. Even I've installed it as global one too the events are
doubled... but I can see that the test observer shows the test status
followed by the call to failure_diagnostic - so the concept works.
To reduce it: How can I access a member function of global fixture on a
compilation unit layout shown above.
Thanks,
Olaf
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net