如何在Visual Studio C ++管理单元测试项目中使用代码注入模型

时间:2017-05-17 13:08:25

标签: c++ visual-studio unit-testing

这是我的Employee.h文件

#include<iostream>
#include<stdlib.h>
#include<vector>
#include<fstream>
#include <cstdio>
#include<algorithm>
#include<iterator>
#include<iomanip>
#define SIZEOF(array)   (int)sizeof(array)
#define sort  stable_sort
using namespace std;

struct employee_rec
{
char name[30];
char department[15];
double salary;
};

 class VEMPLOYEE : public vector<employee_rec>
{
public:
VEMPLOYEE();
~VEMPLOYEE();

void InitEmployee(employee_rec& emp);
    //This method loads the person details from input.txt file
void LoadAndProcessDetails(employee_rec& emp, char* Inputfile, double value);

//This method increments the particular percentage of a salary
void IncrementSalary(employee_rec& emp, double value);

//This method writes the Results to output file
void WriteDetails(employee_rec& emp, char* Outputfile);

void PrintToOutFile(ofstream& outfile, const employee_rec* w);

ifstream& ReadFile(ifstream& infile, employee_rec& emp);    
static inline bool Cmpemp(const employee_rec& x, const employee_rec& y);

};

我的主要员工.cpp

#include"EmployeeManagement.h"

VEMPLOYEE::VEMPLOYEE()
{

}
VEMPLOYEE::~VEMPLOYEE()
{

}

VEMPLOYEE Actualemployee;
employee_rec emp;
VEMPLOYEE MyTestvector;
void VEMPLOYEE::InitEmployee(employee_rec& emp)
{   
strcpy_s(emp.department, " ");
strcpy_s(emp.name, " ");
emp.salary = 0.0;
}
ifstream& VEMPLOYEE::ReadFile(ifstream& infile, employee_rec& emp)
{
infile >> emp.name >> emp.department >> emp.salary;
return infile;
}

void VEMPLOYEE::LoadAndProcessDetails(employee_rec& emp, char* Inputfile, double value)
{
int i = 0;
int ops = 0;
ifstream infile;
InitEmployee(emp);
infile.open(Inputfile, ios::in);
while (ReadFile(infile, emp))
{  
    ++ops;
    IncrementSalary(emp, value);
    Actualemployee.push_back(emp);
}
std::sort(Actualemployee.begin(), Actualemployee.end(), Cmpemp);
infile.close();
}

bool VEMPLOYEE::Cmpemp(const employee_rec& x, const employee_rec& y)
{
int n;
n = strcmp(x.department, y.department);
if (n<0) return 1;
if (n == 0)
{
    n = strcmp(x.name, y.name);
    if (n<0) return 1;
    if (n == 0 && x.salary < y.salary) return 1;
}
return 0;
}

void VEMPLOYEE::IncrementSalary(employee_rec& emp, double value)
{
emp.salary += ((emp.salary / 100) * value);
}

void VEMPLOYEE::WriteDetails(employee_rec& emp, char* Outputfile)
{
vector<employee_rec>::iterator iter1;
ofstream outfile;
outfile.open(Outputfile, ios::out);
for (iter1 = Actualemployee.begin(); iter1 != Actualemployee.end(); ++iter1)
{

    PrintToOutFile(outfile, &*iter1);
}
        outfile.close();
}

void VEMPLOYEE::PrintToOutFile(ofstream& outfile, const employee_rec* w)
{
outfile << setw(15) << left << w->name << setw(7) << w->department << setw(7) << w->salary << "\n";
}

int main()
{
VEMPLOYEE inc;
double value;
employee_rec emp1;
cout << "Enter which percent to increment \n";
cin >> value;
inc.LoadAndProcessDetails(emp, "Input.txt", value);
inc.WriteDetails(emp, "Output.txt");
return 0;
}

Input.txt文件

Dany IT 10000
Loky IT 15000
Rony IT 25000

来自Visualstudio2015 C ++托管测试项目 Employee_Test.cpp文件是

#include "stdafx.h"
#include "Employee.h"
using namespace System;
using namespace System::Text;
using namespace System::Collections::Generic;
using namespace Microsoft::VisualStudio::TestTools::UnitTesting;
std::ofstream LogFile;

namespace EmployeeManagementSystem_MDM_Test
{
[TestClass]
public ref class EmployeeManagement_Testing 
{
private:
    TestContext^ testContextInstance;

public: 
    /// <summary>
    ///Gets or sets the test context which provides
    ///information about and functionality for the current test run.
    ///</summary>
    property Microsoft::VisualStudio::TestTools::UnitTesting::TestContext^ TestContext
    {
        Microsoft::VisualStudio::TestTools::UnitTesting::TestContext^ get()
        {
            return testContextInstance;
        }
        System::Void set(Microsoft::VisualStudio::TestTools::UnitTesting::TestContext^ value)
        {
            testContextInstance = value;
        }
    };

        [TestInitialize]
        void TestInitialize()
        {
            char* testName = (char*)Marshal::StringToHGlobalAnsi(TestContext->TestName).ToPointer();
            LogFile << "Test Case " << testName << " Started Executing" << std::endl;
            Marshal::FreeHGlobal((IntPtr)testName);
        }

        [TestCleanup]
        void TestCleanup()
        {
            char* testName = (char*)Marshal::StringToHGlobalAnsi(TestContext->TestName).ToPointer();
            LogFile << "Test Case " << testName << " Completed Executing" << std::endl;
            Marshal::FreeHGlobal((IntPtr)testName);
        }

        [TestMethod]
        void LoadAndProcessDetails_Test()
        {
            ifstream infile;
            int i = 0;
            size_t n = 9;
            VEMPLOYEE Expectedemployee;
            employee_rec    emp_Test;
            infile.open("Output_Expected.txt",ios::in);
            if (!infile)
            exit(1);

            while (!infile.eof())
            {
                infile >> emp_Test.name >>  emp_Test.department >> emp_Test.salary;

                Expectedemployee.push_back(emp_Test);
            }

            infile.close();             

            VEMPLOYEE inc1;
            employee_rec emp1;
            inc1.LoadAndProcessDetails(emp1, "Input.txt", 10);
            ofstream outfile;
            Assert::AreEqual(Expectedemployee.size(), Actualemployee.size());
        };
    };
}

ExpectedOutput.txt(工资增加10%)

    Dany IT 11000
    Loky IT 16500
    Rony IT 27500

我的C ++项目员工管理系统它从文本文件(Input.txt)读取员工详细信息并将工资增加n百分比,结果打印在文本文件(Output.txt)中。

TestProject: -

在visual studio 2015 Manged C ++ Test Project中,我为我的实际项目创建了测试方法,在实际项目中测试了该函数。

我想做的是

- &gt;测试实际方法并检查员工项目的功能

我的目标: -

1.我创建了ExpectdOutput.txt文件,并与main中的Original输出进行比较   项目

2.如果ActualOutput.txt(来自主程序)和ExpectedOutput.txt(来自我的测试项目)文件有任何不同之处,那些值会转储到一个文本文件中。

我只能测试我的expectedOutpt(来自文本项目)大小和ActualOutput(来自主项目)的大小。

但我想比较所有记录(每个)以及单独和不匹配的值打印到文本文件中??

实现我想在我的测试项目中添加代码的目标(Employee_testing)[但我不想在我的实际Employee Project中更改任何代码]。 请给我任何建议??

0 个答案:

没有答案