I have a function changeUserPassword() in user.cpp and I wanted to do a cppUnit test on it.
here are some possible unit test for changeUserPassword
1. changing a password several times.
2. changing password to the same password.
3. changing a non existing password.
4. Changing the password on one with illegal characters.
The problem is, I feel that the way I wrote my test case for changeUserPassword() isn't testing for anything. It's more like running the method and once it finished, it will return 0.I am quite lost of how to use my method and test against those requirements, How should I or what should I do to improve on the test case?
below are my codes, thanks
user.cpp
int User::changeUserPassword()
{
std::vector<user>::iterator it;
std::ifstream readFile("info.txt");
while(readFile >> userName >> password)
{
userDetails.push_back(user(userName,password));
}
readFile.close();
std::cout << "Please enter a user name that the password will be reset \n";
std::cin >> name;
it = std::find(userDetails.begin(),userDetails.end(),user(name,name));
if (it !=userDetails.end())
{
std::cout << "Please enter a new password" << std::endl;
std::cin >> newPassword;
it->setPassword(newPassword);
std::ofstream out("tempFile.txt");
for (it =userDetails.begin(); it !=userDetails.end(); it++) {
std::cout << it->getUserName() << " " << it->getPassword() << "\n";
out << it->getUserName() << " " << it->getPassword() << std::endl;
}
out.close();
remove("info.txt");
rename("tempfile.txt","info.txt");
}
else
{
it++;
}
return 0;
}
testcase.h
#ifndef TESTCASE_H
#define TESTCASE_H
#include "user.h"
#include <cppunit/TestCase.h>
#include <cppunit/extensions/HelperMacros.h>
class csci222TestCase : public CPPUNIT_NS::TestFixture {
CPPUNIT_TEST_SUITE(testcase);
CPPUNIT_TEST (testChangePassword);
CPPUNIT_TEST_SUITE_END();
public:
protected:
void testChangePassword(void);
private:
user testChangeUserPassword;
};
#endif
testcase.cpp
void testcase::testChangePassword(void) {
std::cout << "\n";
CPPUNIT_ASSERT_EQUAL(testChangeUserPassword.changeUserPassword(),0);
}