简体   繁体   English

如何在Googletest框架的多个测试中将全局变量用于“配置”

[英]How to use a global variable for “configurations” in multiple tests in Googletest framework

I am using Google test framework for C++. 我正在使用C ++的Google测试框架。 Each file includes a config.hpp which defined a global configuration variable. 每个文件都包含一个config.hpp,它定义了一个全局配置变量。 I would like to define my config in a variable, not a compile-time const or constexpr . 我想在变量而不是编译时const或constexpr定义我的配置。 How can I define the dependencies to have the same variable in different files that are linked together? 如何定义依赖项,以便在链接在一起的不同文件中具有相同的变量? Do I have to use a singleton? 我必须使用单身人士吗? Can I avoid that? 我可以避免吗? Is there a better recommended way to use multiple test files xUnit style? 有没有更好的推荐方法来使用多个测试文件的xUnit样式?

My config file: config.hpp : 我的配置文件: config.hpp

#pragma once
struct {
    const float tolerance = 0.001;
    // ...
} CONFIG_VAR;

Each test *.cpp source file is like: 每个测试* .cpp源文件都类似于:

#include "gtest/gtest.h"
#include "core/config.hpp"
TEST(a, b) { ... }

My main file: 我的主文件:

#include "gtest/gtest.h"
int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

I compile and link using: 我使用以下命令进行编译和链接:

em++  -I $GTEST_ROOT/googletest/include main_all_tests.cpp test_*.cpp

PS. PS。 My problem is multiple definition of the variable CONFIG_VAR. 我的问题是变量CONFIG_VAR的多个定义。

My solution is based on a related question . 我的解决方案基于一个相关的问题

Everything you need is right here at the Google Test's official repository on GitHub. 你需要的一切, 在这里在谷歌测试的GitHub上的官方资料库。

Anyway, to sharing something in the same file test you do it like that: 无论如何,要在同一文件测试中共享某些内容,您需要这样做:

class YourTestCase : public ::testing::Test
{
protected:

    virtual void SetUp()
    {
        globalObject = new YourObject();
    }

    virtual void TearDown() {
        delete globalObject;
        globalObject = nullptr;
    }

    Object * globalObject = nullptr;

};

so, in your test cases: 因此,在您的测试用例中:

TEST_F(YourTestCase, TestOne) {

    ASSERT_EQ("your value here", globalObject->getValue());

}

TEST_F(YourTestCase, TestTwo) {

    ASSERT_EQ("your value here", globalObject->getValue());

}

TEST_F(YourTestCase, TestThree) {

    ASSERT_EQ("your value here", globalObject->getValue());

}

Note.: Pay attention to the function's name. 注意:请注意函数的名称。 It is TEST_F not TEST . 它是TEST_F而不是TEST

On the other hand, if what you want to do it is at the test program level ― sharing something among files, you will need to set up an environment object. 另一方面,如果要在测试程序级别进行操作(在文件之间共享某些内容),则需要设置一个环境对象。 Something like this: 像这样:

Environment * AddGlobalTestEnvironment(Environment * env);

I have never worked with that before, so I can not tell you so much about it, but there is more information at that link I shared above. 我以前从未使用过它,所以我不能过多地告诉您,但是我在上面共享的链接中有更多信息。 Usually, global variables make the code harder to read and may cause problems. 通常,全局变量使代码更难阅读,并可能导致问题。 You'd be better off avoiding them. 最好避免它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM