用于testsuite属性的Gtest RecordProperty()

时间:2017-11-21 10:43:31

标签: xml googletest test-suite

使用Google测试框架进行单元测试,是否可以配置输出XML文件属性?我知道'test case'属性是可能的。是否可以配置XML文件的顶级属性('testsuite''testsuites')?

这是我从文档中得到的:允许在测试生命周期之外调用RecordProperty()。如果它在测试之外但在测试用例的SetUpTestCase()TearDownTestCase()方法之间调用,则它将归因于测试用例的XML元素。如果在所有测试用例之外调用它(例如在测试环境中),它将被归因于顶级XML元素。

它说这是可能的,但却无法让它如何运作。准确使用的是RecordProperty

2 个答案:

答案 0 :(得分:1)

这是一个基本的例子。

<强> 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();
}

编译和链接:

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

使用XML输出运行:

$ ./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.

XML测试报告是:

$ 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>

其中<testsuites>具有属性GlobalProperty="TopLevel"

答案 1 :(得分:0)

查看gtest的source code,我找到了函数RecordProperty()的以下解释,它准确地回答了您的问题:

  

在测试的生命周期内(从构造函数启动到析构函数完成的那一刻)调用RecordProperty()将作为<testcase>元素的属性以XML格式输出。从fixture SetUpTestCase()TearDownTestCase()记录的属性将记录为相应<testsuite>元素的属性。在全局上下文中调用RecordProperty()(在调用RUN_ALL_TESTS之前或之后以及从使用Google Test注册的Environment对象的SetUp() / TearDown()方法)将作为属性输出<testsuites>元素。

此外,以下示例阐明了前面提到的规范:

<强> gtest_example.cpp

#include <gtest/gtest.h>

struct exampleFixture : ::testing::Test {
  static void SetUpTestCase() { RecordProperty("TestsuiteProperty", "I am an attribute of TESTSUITE."); }
  void SetUp() { RecordProperty("TestCaseProperty1", "I am an attribute of TESTCASE, added from exampleFixture::SetUp()"); }
};

TEST_F(exampleFixture, testFeatureX) {
  RecordProperty("TestCaseProperty2", "I am an attribute of TESTCASE, added from testFeatureX()");
  ASSERT_TRUE(true);
}

TEST_F(exampleFixture, testFeatureY) {
  RecordProperty("TestCaseProperty3", "I am an attribute of TESTCASE, added from testFeatureY()");
  ASSERT_TRUE(true);
}

int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);

    ::testing::Test::RecordProperty("TestsuitesProperty", "I am an attribute of TESTSUITES");   

    return RUN_ALL_TESTS();
}

使用命令行参数--gtest_output=xml:report.xml运行已编译的可执行文件将生成文件 report.xml

<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="2" failures="0" disabled="0" errors="0" timestamp="2018-02-18T17:11:33" time="0" 
   TestsuitesProperty="I am an attribute of TESTSUITES" name="AllTests">
  <testsuite name="exampleFixture" tests="2" failures="0" disabled="0" errors="0" time="0"
      TestsuiteProperty="I am an attribute of TESTSUITE.">
    <testcase name="testFeatureX" status="run" time="0" classname="exampleFixture"
      TestCaseProperty1="I am an attribute of TESTCASE, added from exampleFixture::SetUp()" 
      TestCaseProperty2="I am an attribute of TESTCASE, added from testFeatureX()" />
    <testcase name="testFeatureY" status="run" time="0" classname="exampleFixture" 
      TestCaseProperty1="I am an attribute of TESTCASE, added from exampleFixture::SetUp()" 
      TestCaseProperty3="I am an attribute of TESTCASE, added from testFeatureY()" />
  </testsuite>
</testsuites>