Tuesday, January 22, 2008

Excluding Certain NUnit Tests

Another developer in my group wrote a group of Automation Tests that verify the conversion of some data within our database. He added these tests to our existing Testing assembly (. We are also using NAnt to run and validate the tests in our Testing assembly as part of our build process. However, we did not want this test to be included as part of the normal tests run during the build process. So to accommodate this we added a category of DatabaseConversion to the test fixture:

[TestFixture]

[Category("DatabaseConversion")]

Then I modified the exclude list for the NAnt task as follows:

<exec program="nunit-console.exe" basedir="${nunit.dir}" workingdir="${working.dir}">
  <arg value="${testassembly}" />
  <arg value="/xml:UnitTests.xml" />
  <arg value="/exclude:DatabaseConversion" />
</exec>

As I thought about this, I was worried about the fact that if someone forgets to put the appropriate category on a test or we decide that another category of tests needs to be excluded we would need to update the NAnt task again. So I went and did some digging in the NUnit documentation and found that there is an Explicit attribute that you can add at the TestFixture or Test level. The ExplictAttribute documentation describes this as follows:

"The Explicit attribute causes a test or test fixture to be ignored unless it is explicitly selected for running. The test or fixture will be run if it is selected in the gui, if its name is specified on the console runner command line as the fixture to run or if it is included by use of a Category filter.

An optional string argument may be used to give the reason for marking the test Explicit.

If a test or fixture with the Explicit attribute is encountered in the course of running tests, it is skipped unless it has been specifically selected by one of the above means. The test does not affect the outcome of the run at all: it is not considered as ignored and is not even counted in the total number of tests. In the gui, the tree node for the test remains gray and the status bar color is not affected."

So now the code looks like the following:

[TestFixture, Explicit]

[Category("DatabaseConversion")]

This is perfect for what we are trying to accomplish as it basically will exclude the test unless it is explicitly selected to be run. And I do not need to be worried about the exclude list for the NAnt task having .

No comments: