Search code examples
xmlgoogletesttest-suite

Gtest RecordProperty() for testsuite attribute


Using Google test framework for unit testing, is it possible to configure the output XML file attributes? I know it's possible for 'test case' attribute. Is it possible to configure the top level attributes('testsuite' or 'testsuites') of the XML file?

This is what I got from the documentation: Calling RecordProperty() outside of the lifespan of a test is allowed. If it's called outside of a test but between a test case's SetUpTestCase() and TearDownTestCase() methods, it will be attributed to the XML element for the test case. If it's called outside of all test cases (e.g. in a test environment), it will be attributed to the top-level XML element.

It says it's possible, but couldn't get how to make it work. Where exactly is the RecordProperty to be used?


Solution

  • Here's an elementary example.

    gtester.cpp

    #include <gtest/gtest.h>
    #include <iostream>
    
    struct my_fixture : ::testing::Test
    {
        void SetUp() {
            std::cout << "Calling " << __PRETTY_FUNCTION__ << std::endl;
    
        }
        void TearDown() {
            std::cout << "Calling " << __PRETTY_FUNCTION__ << std::endl;
        }    
    };
    
    TEST_F(my_fixture,foo)
    {
        ASSERT_EQ(1,1);
    }
    
    
    int main(int argc, char **argv) {
        ::testing::InitGoogleTest(&argc, argv);
        ::testing::Test::RecordProperty("GlobalProperty", "TopLevel");
        return RUN_ALL_TESTS();
    }
    

    Compile and link:

    g++ -Wall -Wextra -pedantic -o gtester gtester.cpp -pthread -lgtest
    

    Run, with XML output:

    $ ./gtester --gtest_output=xml:./gtester.xml
    [==========] Running 1 test from 1 test case.
    [----------] Global test environment set-up.
    [----------] 1 test from my_fixture
    [ RUN      ] my_fixture.foo
    Calling virtual void my_fixture::SetUp()
    Calling virtual void my_fixture::TearDown()
    [       OK ] my_fixture.foo (0 ms)
    [----------] 1 test from my_fixture (0 ms total)
    
    [----------] Global test environment tear-down
    [==========] 1 test from 1 test case ran. (0 ms total)
    [  PASSED  ] 1 test.
    

    And the XML test report is:

    $ cat gtester.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <testsuites tests="1" failures="0" disabled="0" errors="0" timestamp="2017-11-22T19:24:53" time="0" GlobalProperty="TopLevel" name="AllTests">
      <testsuite name="my_fixture" tests="1" failures="0" disabled="0" errors="0" time="0">
        <testcase name="foo" status="run" time="0" classname="my_fixture" />
      </testsuite>
    </testsuites>
    

    in which <testsuites> has the property GlobalProperty="TopLevel".