C++ CppUnit 测试 (CPPUNIT_ASSERT)
我正在尝试完成一项屏幕抓取任务。我的 cpp 可以工作,但我不知道如何集成我的单元测试。我尝试对文件有效性进行布尔检查单元测试,但它给了我这个错误:
error: cannot call member function 'bool ScreenScrape::getFile()' without object
screenscrape.cpp:
#include "screenscrape.h"
using namespace std;
int main()
{
ScreenScrape ss;
int choice;
...
...
ss.matchPatternTest();
}
screenscrape.h:
class ScreenScrape
{
public:
ScreenScrape();
void parserTest(int choice);
void matchPatternTest();
void setIndexValue(string data, string IndexName);
void setIndexChange(string data);
void setIndexPercent(string data);
void setIndexDate(string data);
bool getFile();
private:
string IndexName;
string IndexValue;
string IndexChange;
string IndexPercent;
string IndexVID;
string IndexCID;
string IndexPID;
string IndexDate;
};
bool ScreenScrape::getFile()
{
string file1 = "yahoofinance.htm";
char* file2 = new char [file1.size()+1]; // parse file for c string conversion
strcpy(file2, file1.c_str()); // converts to c string
ifstream fin;
fin.open(file2);
if(fin.good())
return true;
else
return false;
}
screenscrapetest.cpp:< /strong>
#include "screenscrapetest.h"
#include "screenscrape.h"
CPPUNIT_TEST_SUITE_REGISTRATION (ScreenScrapeTest);
void ScreenScrapeTest::fileTest()
{
CPPUNIT_ASSERT(ScreenScrape::getFile()); // test file validity
}
screenscrapetest.h:
#ifndef _SCREENSCRAPETEST_H
#define _SCREENSCRAPETEST_H
#include <cppunit/TestCase.h>
#include <cppunit/extensions/HelperMacros.h>
#include "screenscrape.h"
class ScreenScrapeTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE (ScreenScrapeTest);
CPPUNIT_TEST (fileTest);
CPPUNIT_TEST_SUITE_END ();
public:
void fileTest();
};
#endif
我试图声明“ScreenScrape ss;”在 screencrapetest.h 下,使用对象(ss)调用 getFile() 但它给了我多个此错误:
/home/user/NetBeansProjects/Assignment1/screenscrape.h:259: multiple definition of `ScreenScrape::getFile()'
我只想通过单元测试检查文件有效性。任何帮助将不胜感激。 提前致谢!
问候, 华莱士
I'm trying to do up a screen scraping assignment. My cpp works, but I don't know how to integrate my unit testing. I tried to do a bool check unit test for the file validity but it's giving me this error:
error: cannot call member function 'bool ScreenScrape::getFile()' without object
screenscrape.cpp:
#include "screenscrape.h"
using namespace std;
int main()
{
ScreenScrape ss;
int choice;
...
...
ss.matchPatternTest();
}
screenscrape.h:
class ScreenScrape
{
public:
ScreenScrape();
void parserTest(int choice);
void matchPatternTest();
void setIndexValue(string data, string IndexName);
void setIndexChange(string data);
void setIndexPercent(string data);
void setIndexDate(string data);
bool getFile();
private:
string IndexName;
string IndexValue;
string IndexChange;
string IndexPercent;
string IndexVID;
string IndexCID;
string IndexPID;
string IndexDate;
};
bool ScreenScrape::getFile()
{
string file1 = "yahoofinance.htm";
char* file2 = new char [file1.size()+1]; // parse file for c string conversion
strcpy(file2, file1.c_str()); // converts to c string
ifstream fin;
fin.open(file2);
if(fin.good())
return true;
else
return false;
}
screenscrapetest.cpp:
#include "screenscrapetest.h"
#include "screenscrape.h"
CPPUNIT_TEST_SUITE_REGISTRATION (ScreenScrapeTest);
void ScreenScrapeTest::fileTest()
{
CPPUNIT_ASSERT(ScreenScrape::getFile()); // test file validity
}
screenscrapetest.h:
#ifndef _SCREENSCRAPETEST_H
#define _SCREENSCRAPETEST_H
#include <cppunit/TestCase.h>
#include <cppunit/extensions/HelperMacros.h>
#include "screenscrape.h"
class ScreenScrapeTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE (ScreenScrapeTest);
CPPUNIT_TEST (fileTest);
CPPUNIT_TEST_SUITE_END ();
public:
void fileTest();
};
#endif
I tried to declare "ScreenScrape ss;" under screenscrapetest.h, use an object (ss) to call getFile() but it's giving me multiples of this error:
/home/user/NetBeansProjects/Assignment1/screenscrape.h:259: multiple definition of `ScreenScrape::getFile()'
I only want to check for file validity with unit testing. Any help will be appreciated.
Thanks in advance!
Regards,
Wallace
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
bool ScreenScrape::getFile()
不是static
,因此不能作为静态函数调用。您需要 (a) 将其声明为static
或 (b) 创建ScreenScrape
的实例并从中调用getFile()
。看看代码,并不明显为什么这个函数是类的方法,但也许它仍处于开发的早期阶段。它还可以重构以删除大量冗余代码:
不要忘记
screenscrape.h
中的包含防护:并考虑移动
getFile
的实现code> 到 cpp 源文件。这两个步骤将防止您出现“多重声明”错误。这将修复您的编译错误,但检查文件有效性不是单元测试的责任。单元测试不应与文件系统交互。
bool ScreenScrape::getFile()
is notstatic
, so cannot be called as a static function. You'll need to either (a) declare it asstatic
or (b) create an instance ofScreenScrape
and callgetFile()
from it.Looking at the code, it's not obvious why this function is a method of the class but perhaps it's still in the early stages of development. It can also be refactored to remove lots of redundant code:
Don't forget your include guards in
screenscrape.h
:And consider moving the implementation of
getFile
to the cpp source file. These two steps will prevent you getting the "multiple declaration" errors.This will fix your compilation errors, but checking for file validity is not a responsibility of a unit test. Unit tests should not interact with the filesystem.
如果您要调用
ScreenScrape::getfile()
而不是ss.getfile()
,则需要定义getfile()
作为静态。您收到的错误是因为需要在特定对象上调用非静态方法。如果您的版本定义了 ScreenScrape 对象,然后使用该对象调用
getfile()
,则很难追踪到错误;您显然没有包含所有相关代码,因为您的 screencrape.h 文件没有 259 行,并且您也没有显示“使用对象 (ss) 调用 getFile()”的修改后的代码。If you're going to be calling
ScreenScrape::getfile()
rather thanss.getfile()
, thengetfile()
needs be defined as static. The error you're getting is because non-static methods need to be called on a specific object.It's difficult to track down the error with your version that defines a ScreenScrape object and then uses that to call
getfile()
; you obviously haven't included all the relevant code since your screenscrape.h file doesn't have 259 lines, and you also haven't shown the revised code in which you "use an object (ss) to call getFile()".