如何使 Google 测试正常运行。测试总是失败。 (它不会编译)
How to get Google Test to function properly. Test always fails. (It won't compile)
我的 Google 测试应用程序中的单元测试出现问题。如果我的代码收到错误的日期或错误的日期格式,我想测试它是否会产生异常。我对它使用了正则表达式。我正在使用 Google 测试框架 v1.10.0 和 TDM GCC(GNU G++ GCC C++ 编译器 9.2.0)和 CodeBlocks IDE 20.03。我正在使用 C++ 11。使用 Windows 10.
编译时出现这个错误:
||=== Build: Debug in GT_wsDB_Record_Class (compiler: GNU GCC Compiler) ===|
C:\Users\Donald\Documents\CodeBlocks\GT_Original\GT_wsRecord\GT_wsDB_Record_Class\GT_wsRecord_Test.cpp||In member function 'virtual void wsRecords::wsRecordTest_DoesItThrowExceptionWhenWrongDateIsProvided_Test::TestBody()':|
C:\Users\Donald\Documents\CodeBlocks\GT_Original\GT_wsRecord\GT_wsDB_Record_Class\GT_wsRecord_Test.cpp|28|error: expected ';' before '{' token|
..\..\..\GoogleTest\googletest\googletest\include\gtest\internal\gtest-internal.h|1290|note: in definition of macro 'GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_'|
..\..\..\GoogleTest\googletest\googletest\include\gtest\gtest.h|1970|note: in expansion of macro 'GTEST_TEST_THROW_'|
C:\Users\Donald\Documents\CodeBlocks\GT_Original\GT_wsRecord\GT_wsDB_Record_Class\GT_wsRecord_Test.cpp|28|note: in expansion of macro 'ASSERT_THROW'|
C:\Users\Donald\Documents\CodeBlocks\GT_Original\GT_wsRecord\GT_wsDB_Record_Class\GT_wsRecord_Test.cpp|28|warning: statement has no effect [-Wunused-value]|
..\..\..\GoogleTest\googletest\googletest\include\gtest\internal\gtest-internal.h|1290|note: in definition of macro 'GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_'|
..\..\..\GoogleTest\googletest\googletest\include\gtest\gtest.h|1970|note: in expansion of macro 'GTEST_TEST_THROW_'|
C:\Users\Donald\Documents\CodeBlocks\GT_Original\GT_wsRecord\GT_wsDB_Record_Class\GT_wsRecord_Test.cpp|28|note: in expansion of macro 'ASSERT_THROW'|
||=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 1 second(s)) ===|
我正在尝试学习 TDD,但我似乎无法通过测试。生产代码有效,只是测试无效。我已经努力解决了 1.5 天,所以现在就这样尝试。
希望有人能帮助我。
你好,
唐.
这是主要的测试文件GT_wsRecord_Test.cpp:
/***************************************/
/* wsDB Record Class - UnitTest */
/* Framework: Google Test v1.10.0 */
/* Author: D. Voogd - Date: 11-06-2020 */
/***************************************/
#include <gtest/gtest.h>
#include "GT_wsRecord.h"
#include <string>
namespace wsRecords
{
class wsRecordTest : public ::testing::Test
{
public:
protected:
weatherdayRecord wsRecord{"2020-10-03"};
};
TEST_F(wsRecordTest,DoIGetTheRightTimeFromTheConstructor)
{
std::string wsTime = wsRecordTest::wsRecord.getTime();
ASSERT_EQ(wsTime,"2020-10-03");
}
TEST_F(wsRecordTest,DoesItThrowExceptionWhenWrongDateIsProvided)
{
ASSERT_THROW(wsRecord{"2002-20-41"},std::invalid_argument);
}
}
这是定义文件GT_wsRecord.h:
#ifndef GT_WSRECORD_H_INCLUDED
#define GT_WSRECORD_H_INCLUDED
#include <string>
namespace wsRecords
{
class weatherdayRecord
{
public:
// constructor
weatherdayRecord(std::string time_day);
// destructor
~weatherdayRecord();
// accessors
std::string getTime() const; // get the time from the day.
/*
std::string getWeatherCon() const; // get the weather conditions
double getMinTemp() const; // get the minimum temperature
double getMaxTemp() const; // get the maximum temperature
double getWindSpeed() const; // get the wind speed
std::string getSymbolVar() const; // get the symbol var for the weather icon
*/
protected:
std::string time_day; // date of the day.
/*
std::string weather_condition; // weather condition of the day
double min_temperature; // minimum temperature of the day
double max_temperature; // maximum temperature of the day
double wind_speed; // maximum weed speed in meters/sec.
std::string symbol_var; // symbol that resembles the weather icon
*/
};
}
#endif // GT_WSRECORD_H_INCLUDED
这是实现文件 GT_wsRecord.cpp:
/***************************************/
/* wsDB Record Class - UnitTest */
/* Framework: Google Test v1.10.0 */
/* Author: D. Voogd - Date: 11-06-2020 */
/* Implementation */
/***************************************/
#include <iostream>
#include <regex>
#include "GT_wsRecord.h"
namespace wsRecords{
weatherdayRecord::weatherdayRecord(std::string time_day)
{
this->time_day = time_day;
}
weatherdayRecord::~weatherdayRecord() {
;
}
std::string weatherdayRecord::getTime() const// get the time from the day.
{
auto const regex = std::regex("((?:19|20)\d\d)-(0?[1-9]|1[012])-([12][0-9]|3[01]|0?[1-9])"); // Regex YYYY-MM-DD
bool const TimeDayContainsRegex = std::regex_search(time_day, regex);
if(TimeDayContainsRegex == true)
{
return time_day;
}
else
{
throw std::invalid_argument("Wrong date or dateformat.");
}
}
}
您可能想做这样的事情:
TEST_F(wsRecordTest,DoIGetTheRightTimeFromTheConstructor)
{
weatherdayRecord wsRecord{"2020-10-03"};
std::string wsTime = wsRecord.getTime();
ASSERT_EQ(wsTime,"2020-10-03");
}
TEST_F(wsRecordTest,DoesItThrowExceptionWhenWrongDateIsProvided)
{
weatherdayRecord wsRecord{"2020-20-41"};
ASSERT_THROW(wsRecord.getTime(),std::invalid_argument);
}
您想在第二个测试中测试 getTime()
函数是否抛出异常,因此您首先需要创建一个带有错误日期的 weatherdayRecord
(我认为您正试图在您的原代码)。
请注意,不需要 wsRecord
数据成员。只有当多个测试需要以相同方式构造的相同对象时,夹具数据成员才是必需的。在您的情况下,您需要为每个测试构建不同的 weatherdayRecord
。
我的 Google 测试应用程序中的单元测试出现问题。如果我的代码收到错误的日期或错误的日期格式,我想测试它是否会产生异常。我对它使用了正则表达式。我正在使用 Google 测试框架 v1.10.0 和 TDM GCC(GNU G++ GCC C++ 编译器 9.2.0)和 CodeBlocks IDE 20.03。我正在使用 C++ 11。使用 Windows 10.
编译时出现这个错误:
||=== Build: Debug in GT_wsDB_Record_Class (compiler: GNU GCC Compiler) ===| C:\Users\Donald\Documents\CodeBlocks\GT_Original\GT_wsRecord\GT_wsDB_Record_Class\GT_wsRecord_Test.cpp||In member function 'virtual void wsRecords::wsRecordTest_DoesItThrowExceptionWhenWrongDateIsProvided_Test::TestBody()':| C:\Users\Donald\Documents\CodeBlocks\GT_Original\GT_wsRecord\GT_wsDB_Record_Class\GT_wsRecord_Test.cpp|28|error: expected ';' before '{' token| ..\..\..\GoogleTest\googletest\googletest\include\gtest\internal\gtest-internal.h|1290|note: in definition of macro 'GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_'| ..\..\..\GoogleTest\googletest\googletest\include\gtest\gtest.h|1970|note: in expansion of macro 'GTEST_TEST_THROW_'| C:\Users\Donald\Documents\CodeBlocks\GT_Original\GT_wsRecord\GT_wsDB_Record_Class\GT_wsRecord_Test.cpp|28|note: in expansion of macro 'ASSERT_THROW'| C:\Users\Donald\Documents\CodeBlocks\GT_Original\GT_wsRecord\GT_wsDB_Record_Class\GT_wsRecord_Test.cpp|28|warning: statement has no effect [-Wunused-value]| ..\..\..\GoogleTest\googletest\googletest\include\gtest\internal\gtest-internal.h|1290|note: in definition of macro 'GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_'| ..\..\..\GoogleTest\googletest\googletest\include\gtest\gtest.h|1970|note: in expansion of macro 'GTEST_TEST_THROW_'| C:\Users\Donald\Documents\CodeBlocks\GT_Original\GT_wsRecord\GT_wsDB_Record_Class\GT_wsRecord_Test.cpp|28|note: in expansion of macro 'ASSERT_THROW'| ||=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 1 second(s)) ===|
我正在尝试学习 TDD,但我似乎无法通过测试。生产代码有效,只是测试无效。我已经努力解决了 1.5 天,所以现在就这样尝试。
希望有人能帮助我。
你好,
唐.
这是主要的测试文件GT_wsRecord_Test.cpp:
/***************************************/
/* wsDB Record Class - UnitTest */
/* Framework: Google Test v1.10.0 */
/* Author: D. Voogd - Date: 11-06-2020 */
/***************************************/
#include <gtest/gtest.h>
#include "GT_wsRecord.h"
#include <string>
namespace wsRecords
{
class wsRecordTest : public ::testing::Test
{
public:
protected:
weatherdayRecord wsRecord{"2020-10-03"};
};
TEST_F(wsRecordTest,DoIGetTheRightTimeFromTheConstructor)
{
std::string wsTime = wsRecordTest::wsRecord.getTime();
ASSERT_EQ(wsTime,"2020-10-03");
}
TEST_F(wsRecordTest,DoesItThrowExceptionWhenWrongDateIsProvided)
{
ASSERT_THROW(wsRecord{"2002-20-41"},std::invalid_argument);
}
}
这是定义文件GT_wsRecord.h:
#ifndef GT_WSRECORD_H_INCLUDED
#define GT_WSRECORD_H_INCLUDED
#include <string>
namespace wsRecords
{
class weatherdayRecord
{
public:
// constructor
weatherdayRecord(std::string time_day);
// destructor
~weatherdayRecord();
// accessors
std::string getTime() const; // get the time from the day.
/*
std::string getWeatherCon() const; // get the weather conditions
double getMinTemp() const; // get the minimum temperature
double getMaxTemp() const; // get the maximum temperature
double getWindSpeed() const; // get the wind speed
std::string getSymbolVar() const; // get the symbol var for the weather icon
*/
protected:
std::string time_day; // date of the day.
/*
std::string weather_condition; // weather condition of the day
double min_temperature; // minimum temperature of the day
double max_temperature; // maximum temperature of the day
double wind_speed; // maximum weed speed in meters/sec.
std::string symbol_var; // symbol that resembles the weather icon
*/
};
}
#endif // GT_WSRECORD_H_INCLUDED
这是实现文件 GT_wsRecord.cpp:
/***************************************/
/* wsDB Record Class - UnitTest */
/* Framework: Google Test v1.10.0 */
/* Author: D. Voogd - Date: 11-06-2020 */
/* Implementation */
/***************************************/
#include <iostream>
#include <regex>
#include "GT_wsRecord.h"
namespace wsRecords{
weatherdayRecord::weatherdayRecord(std::string time_day)
{
this->time_day = time_day;
}
weatherdayRecord::~weatherdayRecord() {
;
}
std::string weatherdayRecord::getTime() const// get the time from the day.
{
auto const regex = std::regex("((?:19|20)\d\d)-(0?[1-9]|1[012])-([12][0-9]|3[01]|0?[1-9])"); // Regex YYYY-MM-DD
bool const TimeDayContainsRegex = std::regex_search(time_day, regex);
if(TimeDayContainsRegex == true)
{
return time_day;
}
else
{
throw std::invalid_argument("Wrong date or dateformat.");
}
}
}
您可能想做这样的事情:
TEST_F(wsRecordTest,DoIGetTheRightTimeFromTheConstructor)
{
weatherdayRecord wsRecord{"2020-10-03"};
std::string wsTime = wsRecord.getTime();
ASSERT_EQ(wsTime,"2020-10-03");
}
TEST_F(wsRecordTest,DoesItThrowExceptionWhenWrongDateIsProvided)
{
weatherdayRecord wsRecord{"2020-20-41"};
ASSERT_THROW(wsRecord.getTime(),std::invalid_argument);
}
您想在第二个测试中测试 getTime()
函数是否抛出异常,因此您首先需要创建一个带有错误日期的 weatherdayRecord
(我认为您正试图在您的原代码)。
请注意,不需要 wsRecord
数据成员。只有当多个测试需要以相同方式构造的相同对象时,夹具数据成员才是必需的。在您的情况下,您需要为每个测试构建不同的 weatherdayRecord
。