Translate

Tuesday, 28 July 2009

Parallel Unit Testing with PNUnit

One of the great things about the coming version of NUnit is the integration of PNUnit - the parallel Unit Testing tool developed by codice.com.

With PNunit, you can distribute your tests across machines and test it for different load and stress scenarios. This distributed testing mechanism also helps in performing "smoke tests". The other advantages, to keep it brief, is that the team that uses PNUnit need not learn any new scripting language and can extend on its existing skill sets. This is also why PNUnit is called as NUnit Extensions.

The parallel execution of testsis possible through a configuration based framework where the configuration file (with an extension of ".conf") tells the framework the assembly name, the test fixture name, the machine ip, the port number - much like how a remoting configuration file would do.

The PNUnit framework is enabled by two components - an Agent that listens on a certain port and a Launcher, which launches the Unit Tests as specified in the .conf file.

The ease of use is further enhanced with the help of the familiar C# / NUnit type syntax. The example below shows the execution of the tests on two machines.

Copy the source code and build. The output assembly should be "pnunittests.dll" or even if it is not, make sure to change the name in the ".conf" file.

Copy the configuration code below into a file called "pnunittests.conf" and place all these files into the "pnunit" folder.


// PNUnitTests.dll

using System;
using System.Threading;
using NUnit.Framework;
using PNUnit.Framework;

namespace PNUnitTests
{

[TestFixture]
public class Testing
{
private string[] testParams;
[SetUp]
public void initTests()
{
testParams = PNUnitServices.Get().GetTestParams();
}
[Test]
public void FirstTest()
{

PNUnitServices.Get().InitBarrier("BARRIER");
// wait two seconds
System.Threading.Thread.Sleep(2000);
PNUnitServices.Get().WriteLine(
string.Format(
"FirstTest started with param {0}",
testParams[0]));
PNUnitServices.Get().EnterBarrier("BARRIER");
Assert.AreEqual(Convert.ToInt32(testParams[0]), Cmp.Add(15, 4));
}
[Test]
public void SecondTest()
{
PNUnitServices.Get().WriteLine(
"Second test will wait for first");
PNUnitServices.Get().InitBarrier("BARRIER");
// will wait for the first test
PNUnitServices.Get().WriteLine(
"First test should be started now");
Assert.AreEqual(Convert.ToInt32(testParams[0]), Cmp.Add(15, 4));
}

}
}

//Distributedtest.dll

using System;
using System.Threading;
using NUnit.Framework;
using PNUnit.Framework;

namespace DistributedTest
{
[TestFixture]
public class Class1
{
[Test]
public void TestOnAnotherMachine()
{
string[] testValues=PNUnitServices.Get().GetTestParams();
Assert.AreEqual(testValues[0], "Hello World!");
}
}
}

// pnunittests.conf
<?xml version="1.0" encoding="utf-8" ?>
<TestGroup>
<ParallelTests>
<ParallelTest>
<Name>SimpleTest</Name>
<Tests>
<TestConf>
<Name>FirstTest</Name>
<Assembly>pnunittests.dll</Assembly>
<TestToRun>PNUnitTests.Testing.FirstTest</TestToRun>
<Machine>localhost:8080</Machine>
<TestParams>
<string>19</string>
</TestParams>
</TestConf>
<TestConf>
<Name>SecondTest</Name>
<Assembly>pnunittests.dll</Assembly>
<TestToRun>PNUnitTests.Testing.SecondTest</TestToRun>
<Machine>localhost:8080</Machine>
<TestParams>
<string>19</string>
</TestParams>

</TestConf>
<TestConf>
<Name>DistributedTest</Name>
<Assembly>distributedtest.dll</Assembly>
<TestToRun>DistributedTest.Class1.TestOnAnotherMachine</TestToRun>
<Machine><your ip>:8080</Machine>
<TestParams>
<string>Hello World!</string>
</TestParams>

</TestConf>

</Tests>
</ParallelTest>
</ParallelTests>
</TestGroup>



PNUnit uses a older version of NUnit so use the same NUnit framework version that comes bundled with the PNUnit binary.

You need the PNUnit, .Net 2.0 or above framework and machines to be in the same network for the code to work. Provide for the appropriate ip and port number in the "pnunittests.conf" file.

Download PNUnit and extract the binary to a location. Compile and place the test assemblies(the test code posted above) in the same folder as PNUnit extracted files.

Open Command prompt, change directory to the PNUnit folder. Run "Start Agent agent.conf". The Agent window will open; next, Run "Launcher pnunittests.conf" file, the configuration file provided above.

For testing the "distributedtest.dll" on the remote machine, extract the "PNUnit" binary in that machine, place the .dll file into the same folder as in the local machine and start ONLY the agent in the remote machine. Run "Launcher pnunittests.conf" from the local machine. Default port used in agent.conf is 8080.

Happy Parallel Testing!

1 comment:

RJV said...

It is all tested and executes properly. Open source tools are versioned and can differ with each version.

Do not overreach yourself if you are not well-equipped with open source.

Post with a non-anonymous id and if you cannot, stop visiting my blog because anonymous visitors with such a bad language arent welcome.