mercredi 5 août 2015

Google Tests ValuesIn with a global vector

I'm trying to run a bunch of google tests based off configuration files in a directory. This way I can just add a new file and run the tests without having to add it to my parametrized test and recompile. Here is my code:

typedef std::pair<QString, int> TestParam;

QString generatedLogic;

std::vector<TestParam> badExpressionTests;

class LogicBuilderTest : public ::testing::TestWithParam<TestParam> {};

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

    ::generatedLogic = "/home/mitydsp/trunk/System/logicExecutionEngine/engine/include/GeneratedLogic.h";

    QString dir = "/home/mitydsp/trunk/System/logicExecutionEngine/unittest/expressions/bad/";
    QDirIterator it(dir, QStringList() << "*.txt", QDir::Files, QDirIterator::Subdirectories);

    while (it.hasNext()) {
        QString path = it.next();
        QStringList nameParts = it.fileName().split("_");
        int exitCode = nameParts[0].toInt();
        ::badExpressionTests.push_back(TestParam(path, exitCode));
    }

    std::cout << "Size of vector: " << badExpressionTests.size() << std::endl;

    return RUN_ALL_TESTS();
}

/**
 * Run parameterized test
 */
TEST_P(LogicBuilderTest, TestExitWithCode)
{    
    ::testing::FLAGS_gtest_death_test_style = "threadsafe";

    // Simulate fake main()
    char  arg0[] = "logicTest";
    char* argv[] = { &arg0[0], NULL };
    int argc = (int)(sizeof(argv) / sizeof(argv[0])) - 1;

    EXPECT_EXIT({
        // Need to run Qt Application because logic builder uses async QTimer::singleShot()
        QCoreApplication app(argc, argv);

        // Create a logic builder instance
        LogicBuilder logicBuilder(
            (int) ParameterStoreInterface::DEFAULT_PARAM_STORE_VALUES_KEY,
            (int) ParameterStoreInterface::DEFAULT_PARAM_STORE_NAMES_KEY,
            (int) ParameterStoreInterface::DEFAULT_PARAM_STORE_LOCK_KEY,
            QString(GetParam().first),
            QStringList(""),
            QStringList(generatedLogic),
            QString(LogicBuilder::DEFAULT_INTERMEDIATE_DIR),
            QString(LogicBuilder::DEFAULT_OUTPUT_SRC),
            QString(LogicBuilder::DEFAULT_OUTPUT_LIB),
            true );

        app.exec();
    }, ::testing::ExitedWithCode(GetParam().second), "");
} 

INSTANTIATE_TEST_CASE_P(TestBadExpressions, LogicBuilderTest,
                        ::testing::ValuesIn(::badExpressionTests));

When I run this, it shows that 0 tests are being ran even though the vector says its size is two. How come these parametrized tests are not being ran?

Size of vector: 2
[==========] Running 0 tests from 0 test cases.
[==========] 0 tests from 0 test cases ran. (0 ms total)
[  PASSED  ] 0 tests.

Originally I was running the tests by manually defining the configuration files but I don't like this:

    INSTANTIATE_TEST_CASE_P(TestBadExpressions, LogicBuilderTest,
                        ::testing::Values(
                            TestParam("unittest/expressions/bad/17_no_lhs.txt", LogicBuilder::LB_VARIABLE_NO_LHS),
                            TestParam("unittest/expressions/bad/25_incomplete_rhs.txt", LogicBuilder::LB_PARSE_ERROR)
                        ));

Aucun commentaire:

Enregistrer un commentaire