Cppunit

Feb 4, 2010 at 12:18pm
Hey guys, im facing a problem with my cppunit testing. I did a simple C++ program which basically extract data from a file based on its unique id. I would like to use cppunit to check if the data is correct.

I been reading up on it and i have yet to see any examples of c++ cppunit testing . Can anybody help me in this.
Feb 4, 2010 at 12:27pm
Do you mean unit testing?
Feb 4, 2010 at 12:29pm
Please see
http://cplusplus.com/forum/general/18979/
And please, don't post new thread without previous searching!!!!! :(
Feb 4, 2010 at 12:41pm
Sorry, let me explain furthur. What i am having a problem is , i do not get how the cppunit can read the output from a project and then check that value....

Can anibody show me how the coding would look like in order to extract the output and then compare it to a certain value? i read both links before but i do not really get it.
Feb 4, 2010 at 1:12pm
Hey guys, i am trying to test one of my classes using cppunit.

the class is suppose to extract data out of a certain text file and then displays it...

v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
oid readNasdaq::Nasdaq(fstream&  myfile)
{
    string templine ;
    string line;

    while (getline (myfile,templine) )
    {
        line.append(templine);
    }

    int NasdaqValueID = line.find ("id=\"yfs_l10_^ixic\">" , 0) ;
    int NasdaqValueCount = line.find ("</span></td><td class=\"ticker_down\"><span class=\"streaming-datum\" id=\"yfs_c10_^ixic\">" , 0) ;

    int LocationNasdaqValue = NasdaqValueID + 19 ;
    int LengthOfNasdaqValue = NasdaqValueCount - LocationNasdaqValue ;

string NasdaqValue = line.substr( LocationNasdaqValue , LengthOfNasdaqValue ) ;

   cout << "  " << endl ;
cout << "The Value Index of Nasdaq is " << NasdaqValue << endl ;


int NasdaqValueChangeID = line.find ("id=\"yfs_c10_^ixic\">" , 0 ) ;
int NasdaqValueChangeCount = line.find ("</span></td><td class=\"right_cell ticker_down\"><span class=\"streaming-datum\" id=\"yfs_pp0_^ixic\">" , 0) ;

int LocationNasdaqValueChange = NasdaqValueChangeID + 19 ;
int LengthOfNasdaqValueChange = NasdaqValueChangeCount - LocationNasdaqValueChange ;

string NasdaqValueChange = line.substr (LocationNasdaqValueChange , LengthOfNasdaqValueChange ) ;

cout << "The Value Change for Nasdaq is " << NasdaqValueChange << endl ;


the problem i have with my cppunit is how am i suppose to read the value being outputted and test it...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "financetest.h"
#include "finance.h"



CPPUNIT_TEST_SUITE_REGISTRATION (FinanceTest);



void FinanceTest::setUp()
{
    New = " ";
    NewValue = " " ;


}

void FinanceTest::tearDown()
{
    
}

void FinanceTest::testEquals()
{
    
    }
    


HOw can i continue from here
Last edited on Feb 4, 2010 at 1:13pm
Feb 4, 2010 at 1:24pm
what data are you trying to extract? i think you and suzanne(http://cplusplus.com/member/Suzanne/) have exact same project.. do you know each other?
Feb 4, 2010 at 1:33pm
hmm, nope lol, i dont know her. Basically, we reading information from a website and based on certain id , we will extract the information.

We have to use cppunit for testing, and the information regarding it on c++ is really very little.


the type of data we trying to extract are some values displayed in the text file, like 23.3344 or -233.

What i know is that in cppunit, it will compare the values to check if the test pass or fail. but how do i go about getting it to check the output of the program to the values i input in the cpp unit?
Feb 4, 2010 at 2:13pm
I've used a number of unit testing frameworks including cppunit and none work as well as Boost Test. It has facilities for checking stdout.

However, your design is not very testable. In general, it is best to pass in an ostream reference and write to that. In your program you would pass std::cout. For testing you might pass in a std::ostringstring and then test the results of its buffer.

Test-driven development, where you actually write you unit tests first, has the major benefit that your code naturally comes out testable.
Topic archived. No new replies allowed.