简体   繁体   中英

GoogleTest `expression result unused'

EDIT: per @πάνταῥεῖ feedback, I am editing the question.

Using googletest framework to do parameterized test. For the following line I am getting warnings:

clang++ -isystem ../include -g -Wall -Wextra -pthread -std=c++03 -Wno-unknown-pragmas -Wno-missing-field-initializers -c ../samples/sample7_unittest.cc
../samples/sample7_unittest.cc:15:62: warning: expression result unused [-Wunused-value]
INSTANTIATE_TEST_CASE_P(SomeName, MyTest, ::testing::Values((1, 2)));
                                                             ^
../include/gtest/gtest-param-test.h:1423:66: note: expanded from macro 'INSTANTIATE_TEST_CASE_P'
      gtest_##prefix##test_case_name##_EvalGenerator_() { return generator; } \
                                                                 ^
1 warning generated.

Below is my code, I used the sample7 as starting point

#include "gtest/gtest.h"

using ::testing::TestWithParam;

class MyTest : public TestWithParam<int> {
    public:
        virtual void SetUp() { ch = GetParam(); }
        int ch;
};

TEST_P(MyTest, foo) {
    EXPECT_EQ(ch, 10);
}

INSTANTIATE_TEST_CASE_P(SomeName, MyTest, ::testing::Values((1, 2)));

Below is my makefile:

CXX=clang++
GTEST_DIR = ..
USER_DIR = ../samples
CPPFLAGS += -isystem $(GTEST_DIR)/include
CXXFLAGS += -g -Wall -Wextra -pthread -std=c++03 -Wno-unknown-pragmas -Wno-missing-field-initializers
TESTS = sample7_unittest
GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h $(GTEST_DIR)/include/gtest/internal/*.h
all : $(TESTS)
clean :
    rm -f $(TESTS) gtest.a gtest_main.a *.o

GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)
gtest-all.o : $(GTEST_SRCS_)
    $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c  $(GTEST_DIR)/src/gtest-all.cc

gtest_main.o : $(GTEST_SRCS_)
    $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c $(GTEST_DIR)/src/gtest_main.cc

gtest.a : gtest-all.o
    $(AR) $(ARFLAGS) $@ $^

gtest_main.a : gtest-all.o gtest_main.o
    $(AR) $(ARFLAGS) $@ $^

sample7_unittest.o : $(USER_DIR)/sample7_unittest.cc $(GTEST_HEADERS)
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/sample7_unittest.cc

sample7_unittest : sample7_unittest.o gtest_main.a
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@

I am using LLVM 3.7.1 compiler. The issues is the -std=c++03 flag, without the flag I am not getting the warning. Anything I can do to eliminate it? It looks bad on regression test logs.

Note: I also added an issue here: https://github.com/google/googletest/issues/755

There is an surplus pair of parentheses in:

INSTANTIATE_TEST_CASE_P(SomeName, MyTest, ::testing::Values((1, 2)));

Replace with:

INSTANTIATE_TEST_CASE_P(SomeName, MyTest, ::testing::Values(1, 2));

to remove the warning.

I find the presence or absence of -std-c++03 to be immaterial (clang++3.7, g++ 5.2).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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