开发者

C++ CppUnit Test (CPPUNIT_ASSERT)

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


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.


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()".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜