samedi 4 juillet 2020

Test keeps on failing even though function works

I'm running unit test using with visual studio's native unit test. When I run the function using the same exact code outside the test it works just fine, however, when I run the test it fails.

my test code:

#include "pch.h"
#include "CppUnitTest.h"
#include "../banking/datamanagement.cpp"
#include "CppUnitTest.h"
#include <functional>
#include <assert.h>

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace Tests
{
    TEST_CLASS(Tests)
    {
    public:
        
        TEST_METHOD(TestMethod1)
        {
            datamanagement test("C:\\Users\\wilyx11\\Desktop\\projects\\C++\\Banking_management_system\\banking\\testFile.json");
            int testFileReturn = test.loadfile();
            Assert::AreEqual(1, testFileReturn);
        }
    };
}

My function loadfile()

int datamanagement::loadfile() {

    jsonFile.open(this->filename);
    if (jsonFile.is_open())
    {
        std::cout << "File is loaded" << std::endl;
        return 1;
    }
    else
    {
        std::cout << "File Failed to open" << std::endl;
        return 0;
    }


};

I'm new to c++ so any guidance would be helpful, thank you.

Aucun commentaire:

Enregistrer un commentaire