Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does google test ASSERT_FALSE not work in methods but EXPECT_FALSE does

Tags:

c++

googletest

Both the ASSERT_TRUE and ASSERT_FALSE does not compile in the LibraryTest class with the error.

error C2664: 'std::basic_string<_Elem,_Traits,_Alloc>::basic_string(const std::basic_string<_Elem,_Traits,_Alloc> &)' : cannot convert parameter 1 from 'void' to 'const std::basic_string<_Elem,_Traits,_Alloc> &'

It works since in any TEST_F I use. But the EXPECT_FALSE compiles fine in both the LibraryTest class and the TEST_F methods.

How can I use ASSERT in the method used by a TEST_F?

class LibraryTest : public ::testing::Test
{
public:
    string create_library(string libName)
    {
        string libPath = setup_library_file(libName);

        LibraryBrowser::reload_models();

        ASSERT_FALSE(library_exists_at_path(libPath));
        new_library(libName, libPath);
        ASSERT_TRUE(library_exists_at_path(libPath));
        EXPECT_FALSE(library_exists_at_path(libPath));
        return libPath;
    }
};

TEST_F(LibraryTest, libraries_changed)
{
    string libName = "1xEVTestLibrary";
    string libPath = create_library(libName);
}
like image 518
Donald Herman Avatar asked Jul 31 '13 04:07

Donald Herman


People also ask

Is Google Test header only?

Google Test is not header-only: there are libraries to build. So, as a Visual Studio user, you have essentially two options.

How do I skip a Gtest test?

If you have a broken test that you cannot fix right away, you can add the DISABLED_ prefix to its name. This will exclude it from execution. This is better than commenting out the code or using #if 0 , as disabled tests are still compiled (and thus won't rot).

What is Test_f in Gtest?

TEST_F(TestFixtureName, TestName) { ... statements ... } Defines an individual test named TestName that uses the test fixture class TestFixtureName . The test suite name is TestFixtureName .


2 Answers

If new C++ standard is a part of your project, then you can simply workaround that.

#if __cplusplus < 201103L
#error lambda is not supported
#endif

void create_library(const string &libName, string &libPath) {
  libPath = ...
  []() -> void { ASSERT_FALSE(...); }();
}

Or even redefine those macros:

mygtest.hpp

#include <gtest/gtest.hpp>

#if __cplusplus < 201103L
#error lambda is not supported
#endif

// gtest asserts rebind with the `void` error workaround (C++11 and higher is required)
#undef ASSERT_TRUE
#define ASSERT_TRUE(condition) []() -> void { GTEST_TEST_BOOLEAN_((condition), #condition, false, true, GTEST_FATAL_FAILURE_); }()
#undef ASSERT_FALSE
#define ASSERT_FALSE(condition) []() -> void { GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, GTEST_FATAL_FAILURE_); }()

...
like image 102
Andry Avatar answered Oct 27 '22 00:10

Andry


Functions using any of the gtest assertions need to return void. In your case, you could change your function thus:

void create_library(const string &libName, string &libPath) {
  libPath = ...
  ASSERT_FALSE(...)
}

And use it like this:

TEST_F(LibraryTest, libraries_changed) {
  string libName = "foo";
  string libPath;
  create_library(libName, libPath);
}
like image 25
arne Avatar answered Oct 27 '22 01:10

arne