C++ CppUnit 测试 (CPPUNIT_ASSERT)

发布于 2024-09-10 04:57:05 字数 2427 浏览 4 评论 0原文

我正在尝试完成一项屏幕抓取任务。我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

昇り龍 2024-09-17 04:57:05

bool ScreenScrape::getFile() 不是static,因此不能作为静态函数调用。您需要 (a) 将其声明为 static 或 (b) 创建 ScreenScrape 的实例并从中调用 getFile()

看看代码,并不明显为什么这个函数是类的方法,但也许它仍处于开发的早期阶段。它还可以重构以删除大量冗余代码:

bool ScreenScrape::getFile()
{
    std::ifstream fin("yahoofinance.htm");
    return fin.good();
}

不要忘记 screenscrape.h 中的包含防护

#ifndef SCREENSCRAPE_H
#define SCREENSCRAPE_H
// Class declaration here...
#endif//ndef SCREENSCRAPE_H

并考虑移动 getFile 的实现code> 到 cpp 源文件。这两个步骤将防止您出现“多重声明”错误。

这将修复您的编译错误,但检查文件有效性不是单元测试的责任。单元测试不应与文件系统交互。

bool ScreenScrape::getFile() is not static, so cannot be called as a static function. You'll need to either (a) declare it as static or (b) create an instance of ScreenScrape and call getFile() 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:

bool ScreenScrape::getFile()
{
    std::ifstream fin("yahoofinance.htm");
    return fin.good();
}

Don't forget your include guards in screenscrape.h:

#ifndef SCREENSCRAPE_H
#define SCREENSCRAPE_H
// Class declaration here...
#endif//ndef 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.

说谎友 2024-09-17 04:57:05

如果您要调用 ScreenScrape::getfile() 而不是 ss.getfile(),则需要定义 getfile()作为静态。您收到的错误是因为需要在特定对象上调用非静态方法。

如果您的版本定义了 ScreenScrape 对象,然后使用该对象调用 getfile(),则很难追踪到错误;您显然没有包含所有相关代码,因为您的 screencrape.h 文件没有 259 行,并且您也没有显示“使用对象 (ss) 调用 getFile()”的修改后的代码。

If you're going to be calling ScreenScrape::getfile()rather than ss.getfile(), then getfile() 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()".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文