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);
}
Google Test is not header-only: there are libraries to build. So, as a Visual Studio user, you have essentially two options.
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).
TEST_F(TestFixtureName, TestName) { ... statements ... } Defines an individual test named TestName that uses the test fixture class TestFixtureName . The test suite name is TestFixtureName .
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_); }()
...
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With