Google unit test Set up by default - c++

I am making a unit test system with google unit test in C++. And I noticed that all my set up of the unit test contain the same line, and all my tears down contain other line, equal for all.
I wonder if there is any way to create a set up by default to be called before the actual set up of the any unit test.
#include <gtest.h>
class TestExample : ::testing::Test
{
public:
virtual void SetUp ()
{
//same line for all tests of my system
my_system::clean_system();
//code of specific setup
//...
}
virtual void TearDown ()
{
//Code of specific teardown
//...
my_system::clean_system();
}
};

You can create a Wrapper class i.e. TestWrapper, in which you define the default SetUp() and call to the CustomSetUp()
#include <gtest.h>
class TestWrapper: public ::testing::Test
{
public:
virtual void CustomSetUp() {}
virtual void SetUp ()
{
//same line for all tests of my system
my_system::clean_system();
CustomSetUp(); //code of specific setup
}
};
Then use TestWrapper class in your unit test instead of ::testing::Test and overload CustomSetUp() instead of SetUp()
class TestExample : public TestWrapper
{
public:
virtual void CustomSetUp ()
{
//code of specific setup
//...
}
virtual void TearDown ()
{
//Code of specific teardown
//...
my_system::clean_system();
}
};

Related

GTest - how to prepare data for multiple usage via SetUp method?

I am trying to run some google tests, and I have quite a lot of code to repeat in each test fixture, so I want to make the code as brief as possible, and I would like to use the SetUp method of the child class of Testing::test parent class, but the TEST_F fixtures do not recognize the variables from SetUp
This is the simplest example I can come up with:
class FooTest: public testing::Test
{
protected:
virtual void SetUp() // using void SetUp() override does not help
{
int FooVar = 911;
}
virtual void TearDown()
{
}
};
TEST_F(FooTest, SampleTest)
{
// FooTest::SetUp(); // This does not help as well
EXPECT_EQ(911, FooVar);
}
When I try to compile this code it shows an error that FooVar was not declared in this scope. How can I fix it?
Thank you very much for any help.
FooVar is a local variable inside the SetUp method. If you want to use it in the test fixtures, it needs to be a class member:
class FooTest: public testing::Test
{
protected:
int FooVar;
virtual void SetUp() override
{
this.FooVar = 911;
}
};
In this example, if you are only setting integral types, should just make them const member variables.

How to unit test functions that must occur in pairs?

Quite often I have some functions that must occur in pairs such as
//Example
startSomething()
...
stopSomething()
//Example
openSomething()
...
closeSomething()
while unit testing startSomething() and openSomething() is easy since they require no prior conditions / setup, how should I unit test their counterparts which require the prior to be called?
The majority of unit test frameworks have setup tests which are called before/after all, and/or each, of the test cases. Take NUnit as example:
[TestFixture]
public class MyTest
{
[OneTimeSetup]
public void GeneralSetup()
{ ... } //Called before starts all test cases
[OneTimeTearDown]
public void GeneralTearDown()
{ ... } //Called after all test cases are finished
[Setup]
public void Setup()
{ ... } //Called before each test case
[TearDown]
public void TearDown()
{ ... } //Called after each test case
[Test]
public void Test1()
{ ... }
[Test]
public void Test2()
{ ... }
}

C++ Google Mock - EXPECT_CALL() - expectation not working when not called directly

I'm still pretty new to Google Mock so learning as I go. I've just been adding some unit tests and I've run into an issue where I can't get ON_CALL() to correctly stub a method called from within a method.
The following code outlines what I have;
class simpleClass
{
public:
bool simpleFn1() { return simpleFn2(); }
virtual bool simpleFn2() { return FALSE; }
}
In my unit test I have:
class simpleClassMocked : public simpleClass
{
private:
MOCK_METHOD0(simpleFn2, bool());
}
class simpleClassTests : public ::testing::Test
{
}
TEST_F(simpleClassTests, testSimpleFn2)
{
shared_ptr<simpleClassMocked> pSimpleClass = shared_ptr< simpleClassMocked >(new simpleClassMocked());
ON_CALL(*pSimpleClass, simpleF2()).WillByDefault(Return(TRUE));
// This works as expected - simpleFn2() gets stubbed
pSimpleClass->simpleFn2();
// This doesn't work as expected - when simpleFn1 calls simpleFn2 it's not the stubbed expectation???
pSimpleClass->simpleFn1();
}
I figure I must be missing something obvious here, can anyone help? Thanks!
you'll have to Mark the method as virtual and add a corresponding MOCK function in the simpleClassMocked class
class simpleClass
{
public:
virtual bool simpleFn1() { return simpleFn2(); }
virtual bool simpleFn2() { return FALSE; }
}
Also, you need to put the Mock methods in the public area
class simpleClassMocked : public simpleClass
{
public:
MOCK_METHOD0(simpleFn2, bool());
MOCK_METHOD0(simpleFn1, bool());
}
It will work now

Can we pass parameters to the test cases functions in C++ using CppUnit?

I am using cppunit for testing my c++ code. I have written my test fixture like this
class MainTestFixture : public TestFixture
{
CPPUNIT_TEST_SUITE(MainTestFixture);
CPPUNIT_TEST(Addition);
CPPUNIT_TEST(Multiply);
CPPUNIT_TEST_SUITE_END();
public:
void setUp(void);
void tearDown(void);
protected:
// Test Functions
void Addition(void);
void Multiply(void);
};
Now if I implement test cases like
void MainTestFixture::Addition()
{
// CPPUNIT_ASSERT(condition);
}
void MainTestFixture::Multiply()
{
// CPPUNIT_ASSERT(condition);
}
In the above code, is it possible that I pass parameters to Addition and Multiply functions?
Where as I have made a suite for running this fixture like below
#include "MainTestFixture.h"
CPPUNIT_TEST_SUITE_REGISTRATION(MainTestFixture);
using namespace CPPUNIT_NS;
int main()
{
// informs test-listener about testresults
CPPUNIT_NS::TestResult testresult;
// register listener for collecting the test-results
CPPUNIT_NS::TestResultCollector collectedresults;
testresult.addListener (&collectedresults);
// register listener for per-test progress output
CPPUNIT_NS::BriefTestProgressListener progress;
testresult.addListener (&progress);
// insert test-suite at test-runner by registry
CPPUNIT_NS::TestRunner testrunner;
testrunner.addTest (CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest ());
testrunner.run(testresult);
// output results in compiler-format
CPPUNIT_NS::CompilerOutputter compileroutputter(&collectedresults, std::cerr);
compileroutputter.write ();
// Output XML for Jenkins CPPunit plugin
ofstream xmlFileOut("cppMainUnitTest.xml");
XmlOutputter xmlOut(&collectedresults, xmlFileOut);
xmlOut.write();
// return 0 if tests were successful
return collectedresults.wasSuccessful() ? 0 : 1;
}
No, you can not. Addition() is callback which will be registered in CPPUNIT engine and called by the driver - therefore it should use void(void) interface. Instead you could define your parameters as MainTestFixture's members.
class MainTestFixture : public TestFixture
{
CPPUNIT_TEST_SUITE(MainTestFixture);
CPPUNIT_TEST(Addition);
CPPUNIT_TEST(Multiply);
CPPUNIT_TEST_SUITE_END();
public:
void setUp(void);
void tearDown(void);
protected:
// Test Functions
void init_fixture();
void Addition(void);
void Multiply(void);
protected:
//data members
param1_t m_param1;
param2_t m_param2;
};
void MainTestFixture::init_fixture()
{
m_param1 = ...;
m_param2 = ...;
}
void MainTestFixture::Addition()
{
param1_t res= m_param1 + ...;
// CPPUNIT_ASSERT(condition);
}

2 Questions about nUnit

I have 2 questions about functionality of nunit.
What is the difference between [TestFixtureSetUp] and [SetUp] attributes ?
I am writing a some class with tests and I see that half of my test functions need one setup,
And an another half needs another set up.
How can I have in one class two little different SetUp functions that are called with different functions
Thanks.
Method marked with [TestFixtureSetUp] attribute will be executed once before all tests in current test suite, and method marked with [SetUp] attribute will be executed before each test.
As for class with tests which contains tests requring different set up functions. Just split this class in two - each one with it's own SetUp function.
[TestFixture]
public void TestSuite1
{
[SetUp]
public void SetUp1()
{
...
}
[Test]
public void Test1()
{
...
}
}
[TestFixture]
public void TestSuite2
{
[SetUp]
public void SetUp2()
{
...
}
[Test]
public void Test2()
{
...
}
}
or call SetUp function explicitly
[TestFixture]
public void TestSuite
{
public void SetUp1()
{
...
}
public void SetUp2()
{
...
}
[Test]
public void Test1()
{
SetUp1();
...
}
[Test]
public void Test2()
{
SetUp2();
...
}
}
A TestFixtureSetup method is executed once before any of the test methods are executed. A Setup method is executed before the execution of each test method in the test fixture.
How can I have in one class two
little different SetUp functions that are
called with different functions
You can't have two different SetUp functions in a single class marked as TestFixture. If individual tests need some initialization then it makes sense to put the initialization code inside those function themselves.
I see that half of my test functions
need one setup
I think then you need to factor out the tests...