MSTest with C++: Test Methods are not runnable - c++

I try to use MSTest so as to perform unit tests on native C++ code (so as to try to implement Test Driven Development).
There's an MSTest entry in the "Add New Project" C++ Wizard. It creates some code obviously in C+++/CLI.
However, whenever I try to run these tests, Visual Studio indicates me that the tests are not runnable:
Not Runnable TestMethod2 CargoOCRTests UTA007: Method TestMethod2 defined in class CargoOCRTests.UnitTest1 does not have correct signature. Test method marked with the [TestMethod] attribute must be non-static, public, does not return a value and should not take any parameter. for example: public void Test.Class1.Test().
However I think my two tests functions do respect the prototype VS is complaining about:
namespace CargoOCRTests
{
[TestClass]
public ref class UnitTest1
{
[TestMethod]
void TestMethod1()
{
Assert::IsTrue(true);
};
[TestMethod]
void TestMethod2()
{
Assert::IsTrue(false);
};
};
}
Have you any idea what is the cause ?

You need to mark your test methods as public
public:
[TestMethod]
void TestMethod1() {}

Related

How to mock independent function in gmock C++?

I am using gtest/gmock framework to write unit tests. Following is my sample code.
In common.h,
void PreInit() {
cout<<"Doing Pre-initialization."<<endl;
}
In test.cpp,
#include <common.h>
class SampleComponent {
public:
void Init() {
PreInit();
// Do Initialization
}
}
So far, I have been mocking classes. Any idea how to mock independent functions called by member functions of class?
Thanks in advance.

I am doing unit testing using gtest and gmock frameworks and I need help in stubbing/mocking a external C functions used inside class functions

So I am trying to write test cases for my production code but the coverage is drastically low due to the usage of some external C library which cannot be executed without target hardware, So I have no choice but to stub the same. Now the problem is how to stub a C function ?
My production code : prod_code.cpp
int TargetTestClass::targetFunc()
{
if(externalCFunc() == True)
{
statement1; statement2; statement3; /// and so on
}
}
My testcode.cpp generally contains tests like this
//Fixture for Target Test class
class TargetTestClassFixture : public testing::Test {
TargetTestClass* targetTestClassPtr;
void SetUp() {
targetTestClassPtr = new TargetTestClass();
}
void TearDown() {
delete targetTestClassPtr;
}
};
TEST_F (unitTest, test_001)
{
targetTestClassPtr->targetFunc(); //Need to do something here so that externalCFunc() returns true on call
}
What you can do is to create a source file like my_c_stubs.c where you rudimentary implement your C function. For example, the implementation can just return true. Then don't link original source file with the external C function but rather use your stub file. You should still use the original C header. In this way you won't be able to stub inline functions though. If it is required, some more sophisticated approach is needed.
I found 2 solutions to my problem so I am going to answer the same here.
Solution 1 : This involved changing the target source code. Basically you need to write a wrapper that calls the external C functions like below
Class TargetTestClass{
protected:
int targetFunc();
virtual int externalCFuncWrapper(); // Wrapper
};
//call the external C function from the wrapper
int TargetTestClass::externalCFunctionWrapper(){
return(externalCFunc());
}
//Definition of targetFuc in original question
//Now write a mock class for Target Test Class as usual and mock the wrapper function to return what you want to
class MockTargetTestClass : public TargetTestClass{
public: MOCK_METHOD0(externalCFunctionWrapper, int());
};
//Now use the Mock class as needed
TEST_F ( TargetUnitTest, TestingExternalCFuctionCall)
{
MockTargetTestClass mockTargetTestClassObj;
using ::testing::Return;
using ::testing::_;
Expect_Call(mockTargetTestClassObj, externalCFunctionWrapper())
.WillOnce(Return(1));
Assert_EQ(mockTargetTestClassObj.targetFunc(), 1);
}
Solution 2 : Thanks to #kreynolds, I have looked into Fake Function Framework and implemented as follows :
Class TargetTestClass{
protected:
int targetFunc();
//No Code change needed in target source code
};
//In testcode.cpp
#include <gmock-global/gmock-global.h>
MOCK_GLOBAL_FUNC0(externalCFunc, int());
TEST( Unittest, test002){
using ::testing::Return;
using ::testing::_;
EXPECT_GLOBAL_CALL(externalCFunc, externalCFunc()).WillOnce(Return(1));
TargetTestClass targetFunc; //This does not contain any wrapper
EXPECT_EQ(targetTestClassObj.targetFunc(), 1);
}
I am using the second solution as this does not require any change in my source code and easier to use.
Once again thank you everyone for giving your time.

How to write unit testing case for classes in the grails-app/utils folder in Grails 2.3.0

I write a utility class in the grails-app/utils :
package com.demo.formatter
class FooFormatter {
static def foo() {
return true
}
}
and, write a unit test case in the test/unit :
package com.demo.test.formatter
#RunWith(JUnit4)
class FooFormatterTests {
#Test
void testFoo() {
AssertTrue(FooFormatter.foo())
}
}
and then, run testing case with Intellij Idea IDE, but I always got the error message like following:
Class not found: "com.demo.test.formatter.FooFormatterTests"
Did I do something wrong ?
Thanks
This tests runs great in IntelliJ 12.* (test and the util class being in the same package and tested in v2.2.4)
#RunWith(JUnit4)
class FooFormatterTests {
#Test
void testFoo() {
assert FooFormatter.foo()
}
}
Try to test from console/command line with grails test-app.

for internal method test case method is not accessible

I am writing unit test case for my internal method. I have made necessary changes in AssemblyInfo.cs of my mail class project
[assembly: InternalsVisibleTo("myunitest_assemblyname")]
now i can access my internal method in unit test case method.
but when i compile the code it is giving an error as below
Error 1 'main class project name' does not contain a definition for 'Process' and no extension method 'Process' accepting a first argument of type 'main class project name' could be found (are you missing a using directive or an assembly reference?)
my main class has strong name.
it would be great if some one point where i am missing.
main class structure
namespace Renewals
{
public class StateProcessor
{
internal virtual void PutEmailInQueue(DataTable dataTable)
{
}
}
}
//test class structure
namespace Renewals.Tests.Unit
{
[TestClass]
public class StateProcessorTest
{
[TestMethod]
public void PutEmailInQueueTest()
{
DateTime processingDate = Convert.ToDateTime("27-feb-2013"); ;
StateProcessor stateProcess = new StateProcessor(processingDate);
stateProcess.PutEmailInQueue(new DataTable());
}
}
}
PutEmailInQueue - this method giving me problem.
you wrote that your class use strong name.
I think you have to modify your InternalsVisibleTo() statement with the public key.
e.g.: [assembly: InternalsVisibleTo("friend_signed_B, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e3aedce99b7e10823920206f8e46cd5558b4ec7345bd1a5b201ffe71660625dcb8f9a08687d881c8f65a0dcf042f81475d2e88f3e3e273c8311ee40f952db306c02fbfc5d8bc6ee1e924e6ec8fe8c01932e0648a0d3e5695134af3bb7fab370d3012d083fa6b83179dd3d031053f72fc1f7da8459140b0af5afc4d2804deccb6")]
for more informations see: http://msdn.microsoft.com/en-us/library/bb385180.aspx

Scope Problem with Visual Assert

I'm trying to use Visual Assert for testing. Visual Studio says the method I want to test which is defined inside main.cpp is undefined in the Test Fixture.
MyFunctionTest.cpp:
#include <cfixcc.h>
class ExampleTest : public cfixcc::TestFixture
{
private:
public:
void Test()
{
CFIXCC_ASSERT_EQUALS(4, MyFunction(2,2));
}
};
CFIXCC_BEGIN_CLASS(ExampleTest)
CFIXCC_METHOD(Test)
CFIXCC_END_CLASS()
I didn't make a separate project for tests so the two files are a part of the same project. How can I have MyFunction visible for Visual Assert to work properly?
#include the header that declares MyFunction()