Software QA FYI - SQAFYI

Improve Unit Testing with Visual C++ Programming

By:

Visual Studio comes with an interesting tool for unit testing as described in this blog about Writing Unit Tests in Visual Studio for Native C++, but this solution had the following disadvantages due to using C++\CLI:

* Intellisense not supported for C++\CLI.
* Assert is designed for .Net languages , and using it for native C++ induce using some workarounds like:Assert for string,Assert::IsNullPointer.
* nullptr exist both in C++0x and CLI and concerning test for C++0x code we have to use __nullptr instead.
* Many developers don't understand why they have to use C++\CLI to test native C++.

This testing tool is very elegant; it can easily be plugged into Team Foundation Server TFS), so don't be discouraged by the disadvantages described.

There's a simple solution to the disadvantages, with few changes, that resolve these issues. To explain this solution let's change the following test class.

using namespace System::Text;
using namespace System::Collections::Generic;
using namespace Microsoft::VisualStudio::TestTools::UnitTesting;
namespace TestProject1
{
[TestClass]
public ref class UnitTest1
{
public:
[TestMethod]
void TestMethod1()
{
//some test code
}
};
}


The most feedback about this code is the use of C++\CLI instead of native C++. The goal is to implement an alternative solution to the one proposed by Visual Studio. For that, let's do the following steps: Step1: Create a Native Class for Test

The best aproach to test native code is to use a native test class like any other existing test unit framework (CppUnit,googleTest,...)

Let's create our native test class:

NativeTest.h
class NativeUnitTest1
{
Public:
void TestMethod1();
}

#include "nativetest.h"
void NativeUnitTest1::TestMethod1(){
//Native test code
}

We have to change the "Common Language Support" property to "No Common language rutime support." I n this case the intellisense works perfectly , and all test code is written with native C++.

Step2 : Create NativeAssert Class to Encapsulate Managed Assert Class

For test methods we need to use Assert methods. To avoid the drawbacks of using the managed Assert class, and facilitate using this class for native code, let's create a native class that encapsulates the managed one.

NativeAssert.h file:
class NativeAssert
{
public:
static void AreEqual(int a,int b);
static void AreEqual(std::string a,std::string b);
};

NativeAssert.cpp file:
using namespace Microsoft::VisualStudio::TestTools::UnitTesting;
#include "NativeAssert.h"
void NativeAssert::AreEqual(int a,int b)
{
Assert::AreEqual(a,b);
}
void NativeAssert::AreEqual(std::string a,std::string b)
{
Assert::AreEqual(gcnew System::String(a.c_str()), gcnew System::String(b.c_str()));
}

Of course, all assert methods needed must be added to this class; this class will be developed one time and used by all test cases.

Step3 : Use NariveAssert in the Native Test Methods
void NativeUnitTest1::TestMethod1(){
//some code
NativeAssert("test",p->name());
}

To improve the syntax we can add this define:
#define NativeAssert Assert
So the code will be:
void NativeUnitTest1::TestMethod1(){
//some code
Assert("test",p->name());
}

Full article...


Other Resource

... to read more articles, visit http://sqa.fyicenter.com/art/

Improve Unit Testing with Visual C++ Programming