Visual Studio 2010 Unit Testing in C++

I am trying to make a basic unit test for a very basic C++ class in Visual Studio 2010. I have already tested the class and everything works. However, when I make a Test Project, the class is undeclared.

I feel like I'm missing something basic. Could someone clearly explain the basic steps for creating a unit test in visual studio 2010 in c++?


My class header file:

#include <string>
#include <iostream>
using namespace std;
#ifndef FIRST_H
#define FIRST_H
class First
{
private:
string name;
int age;
public:
// Constructors
First(); // default constructor
First(string n, int a); // constructor with parameters

void ChangeAge(int newAge);
void ChangeName(string newName);

void getName();
void getAge();
};
#endif

My Class cpp file:

#include "First.h"

// Constructors
First :: First()// default constructor
{
name = "No Name";
age = 0;
}
First :: First(string n, int a) // constructor with parameters
{
name = n;
age = a;
}
//Manipulators
void First :: ChangeAge(int newAge)
{
age = newAge;
}
void First :: ChangeName(string newName)
{
name = newName;
}
// Observers
void First :: getName()
{
cout << "The name of this student is " << name << endl;
//cin >> name;
}
void First :: getAge()
{
cout << "The age of this student is " << age << endl;
}

Here is my Basic Unit Test: // Note: All of this is autogenerated, except for the body of void TestMethod1()

#include "stdafx.h"

using namespace System;
using namespace System::Text;
using namespace System::Collections::Generic;
using namespace Microsoft::VisualStudio::TestTools::UnitTesting;

namespace TestProject5
{
[TestClass]
public ref class UnitTest1
{
public:
[TestMethod]
void TestMethod1()
{
First person; // Sets Age to 0
person.getAge(); // Should Display 0
}
};
}

When building the unit Test project, I am getting an error saying:
"First is an undeclared identifier".

If I change the "public ref class" value from "UnitTest1" to my class name "First", I get the following error:
"'getAge' is not a member of 'TestProject5::First'"

You need to include first.h in Basic Unit Test otherwise the compiler won't know about your First class.
BTW Unit test work normally that you check returned values for expected values.
1
2
3
4
5
6
void TestMethod1()
{
  First person; // Sets Age to 0
  // Not sure about the proper name
  //Assert::Equal(0, person.getAge()); // Should Display 0
}

Why is the class called First when it represents a person.
Why is the method called getAge when it prints the age ?
Your code becomes more understandable(even for yourself) when you use proper names.
When I do #include "First.h" (After adding the Project Directory to the Test Project), I get a huge list of 3000+ error messages, to the point where my output box can handle anymore. They all say "this type is not verifiable".
Error 1 error C4956: 'va_list *' : this type is not verifiable
Error 2 error C4956: 'va_list *' : this type is not verifiable
Error 3 error C4956: 'void *(va_list *,...)' : this type is not verifiable
Error 4 error C4956: 'va_list *' : this type is not verifiable
Error 5 error C4956: 'const wchar_t *' : this type is not verifiable
Error 6 error C4956: 'const wchar_t *' : this type is not verifiable
.
.
.
Error 3157 error C1003: error count exceeds 100; stopping compilation


Here is my code: *************************************************************
#include "stdafx.h"
#include "First.h"
using namespace System;
using namespace System::Text;
using namespace System::Collections::Generic;
using namespace Microsoft::VisualStudio::TestTools::UnitTesting;

namespace TestProject4
{
[TestClass]
public ref class UnitTest1
{
public:
[TestMethod]
void TestMethod1()
{

}
};
}
*********************************************************
I also tried including the header file above "stdafx.h", it doesn't work. It skips the declaration of "First.h"
is your test project set up for something nonstandard like managed code that is different from the working code?

if you don't mind an annoying re-do just copy the original project and removed everything but the files you need and add back the driver main and see if you can get that to work as it will retain the same project settings. That might be easier than trying to find a difference.
Last edited on
Topic archived. No new replies allowed.