How to unit test functions that must occur in pairs? - unit-testing

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()
{ ... }
}

Related

Mstest [ClassCleanup] not executed at all in VS 2019

[ClassInitialize]
public static void BeforeCls(TestContext tc)
{
Console.WriteLine("ClassInitialize - Before Class");
}
[ClassCleanup]
public static void AfterCls()
{
Console.WriteLine("ClassCleanup - After Class");
}
[TestInitialize]
public void BeforeMethod()
{
Console.WriteLine("TestInitialize - Before Method");
}
[TestCleanup]
public void AfterMethod()
{
Console.WriteLine("TestCleanup - After Method");
}
[TestMethod]
public void TestMethod1()
{
Console.WriteLine("Test Method - 1");
}
Class cleanup is never executed in this code.
here is the Output
ClassInitialize - Before Class
TestInitialize - Before Method
Test Method - 1
TestCleanup - After Method
Invested a bit more time and discovered that using this will work within a test class:
[ClassCleanup(InheritanceBehavior.None))]
public static void ClassCleanup()
{
// Your code goes here
}
And using that will work for a derived test class:
[ClassCleanup(InheritanceBehavior.BeforeEachDerivedClass)]
public static void ClassCleanup()
{
// Your code goes here
}
You can read more about it here.
If the solution is not working for you no matter what, you can define IDisposable. The code will look something like this:
[TestClass]
public class DisposableBaseTest : IDisposable
{
// Your initialize methods
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~DisposableUserBaseTest()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
// Your cleanup code
Console.WriteLine("ClassCleanup - After Class");
}
}
}
P.S. And according to carlin.scott from this post, if you receive an exception during ClassInitialize, you want to handle this separately.
ClassCleanup is executed - put a breakpoint inside your method and verify it yourself. Problem is, that this method is executed after all of your tests are completed, so you cannot write message from that method in test output report. This is similar to ClassInitialize method. If you have multiple tests in your file, your ClassInitialize will write a message to only one of them.

Google unit test Set up by default

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();
}
};

Robotium setUp() and tearDown() methods

I have a test class as follows. The methods setUp() and tearDown() run before and after each test case. However, I want these methods to run once in the beginning of the test suite and in the end. I could not find an annotation like #BeforeClass, #AfterClass. How can I make these methods to run before the first test case and after the last test case in a test class?
protected void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
}
protected void tearDown() throws Exception {
solo.finishOpenedActivities();
super.tearDown();
}
public void test1{
do smt
}
public void test2{
do smt
}
The tearUp() method should be placed at the bottom, after your test cases. The tearDown() method will automatically be run last.
The setUp() method should be placed before your test cases and it will automatically be run first before your test cases start running.
Like this:
protected void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
}
public void test1{
do smt
}
public void test2{
do smt
}
protected void tearDown() throws Exception {
solo.finishOpenedActivities();
super.tearDown();
}

Gallio unit test startup code

This is going to be an amateur level question. Is there a way to add start up code to a test project that uses MBUnit 3.4.0.0? I tried adding [TestFixture] and [FixtureSetUp] attributes to code that I wanted to run first, but unfortunately that didnt help.
The [FixtureSetUp] should be executed once before any test of the tests contained within the [TestFixture], but the two cannot be used interchangeably.
Here's a quick example. Admittedly the class doesn't have to be decorated with the [TestFixture] attribute, but it's good practice.
[TestFixture]
public class SimpelTest
{
private string value = "1";
[FixtureSetUp]
public void FixtureSetUp()
{
// Will run once before any test case is executed.
value = "2";
}
[SetUp]
public void SetUp()
{
// Will run before each test
}
[Test]
public void Test()
{
// Test code here
Assert.AreEqual("2", value);
}
[TearDown]
public void TearDown()
{
// Will run after the execution of each test
}
[FixtureTearDown]
public void FixtureTearDown()
{
// Will run once after every test has been executed
}
}

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...