<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>NULL.in &#187; testng</title>
	<atom:link href="http://www.nalinmakar.com/tag/testng/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nalinmakar.com</link>
	<description>Nalin's Labyrinth</description>
	<lastBuildDate>Thu, 29 Jul 2010 04:07:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Logging tests to separate files</title>
		<link>http://www.nalinmakar.com/2010/07/28/logging-tests-to-separate-files/</link>
		<comments>http://www.nalinmakar.com/2010/07/28/logging-tests-to-separate-files/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 04:07:34 +0000</pubDate>
		<dc:creator>Nalin</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[logback]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[testng]]></category>

		<guid isPermaLink="false">http://www.nalinmakar.com/?p=655</guid>
		<description><![CDATA[We have been using TestNG to run our tests for a while now. When we initially started, our code was written using Log4J and we were able to configure it to log all the tests to a single file. This made the logs very unwieldy and looking for issues very difficult. Looking at 100+ MB [...]]]></description>
			<content:encoded><![CDATA[<p>We have been using TestNG to run our tests for a while now. When we initially started, our code was written using Log4J and we were able to configure it to log all the tests to a single file. This made the logs very unwieldy and looking for issues very difficult. Looking at 100+ MB of logs isn&#8217;t an easy task. Changing this to rolling files only made the matters worse. So, I started looking for ways to log each test case into a separate file (meaning a test named foo, would be logged to foo.log).</p>
<p>My first attempt was to do this using log4j itself. Log4J doesn&#8217;t provide an easy way of doing this. It&#8217;s possible if you follow certain conventions in declaring your loggers and how you use MDC, but I wasn&#8217;t able to get it working with all my code. On looking a bit further, I found <a  href="http://logback.qos.ch/">Logback</a>. And <a  href="http://logback.qos.ch/manual/appenders.html#SiftingAppender">SiftingAppender</a> in Logback is just what I needed.</p>
<p>So, using <a  href="http://www.slf4j.org/">slf4j</a>, <a  href="http://logback.qos.ch/">logback</a>, <a  href="http://logback.qos.ch/manual/mdc.html">MDC</a> and a few simple coding conventions, I was able to get per test log files working. Here&#8217;s what you need to do:</p>
<ol>
<li>Start using SLF4J for your logging. If you are using Log4J, <a  href="http://www.slf4j.org/migrator.html">moving from L0g4J to SLF4J</a> is pretty simple</li>
<li>Write functions to set MDC at start of a test and unset it at the end of a test</li>
<li>Update the test cases, such that the MDC set/unset functions are invoked</li>
<li>Configure logback.xml to use the SiftingAppender</li>
</ol>
<p>I&#8217;ll expand a bit more on the above.</p>
<h2>Using SLF4J</h2>
<p>Starting to use SLF4J or moving to SLF4J from other logging frameworks is pretty straightforward. I didn&#8217;t bother using the java application at <a  href="http://www.slf4j.org/migrator.html">http://www.slf4j.org/migrator.html</a> and found it much easier to just to regex to do the job. There were some places where <tt>log.xxx(object)</tt> had to be changed to <tt>log.xxx(object.toString())</tt> but that wasn&#8217;t a whole lot of pain.</p>
<h2>Code to set/unset MDC</h2>
<p>Logback&#8217;s documentation on MDC is very extensive and explains the concept quite clearly. Essentially using MDC would allow us to share a key/value across a thread hierarchy. Using MDC is as simple as putting a key/value into a Map. This value will be accessible anytime in the thread and any children of that thread. At the beginning of the test, we&#8217;ll put <tt>&lt;"testname", $test_case_name&gt;</tt> as the key/value into the map. At the end of the test, we&#8217;ll remove this key. This MDC value would be used by the sifting appender to create logs at runtime.  I wrote a simple class to encapsulate this functionality:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.nm.examples</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.slf4j.Logger</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.slf4j.LoggerFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.slf4j.MDC</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Class to handle setting/removing MDC on per test case basis. This helps
 * us log each test case into it's own log file. Please see
 * {@link http://logback.qos.ch/manual/appenders.html#SiftingAppender}
 * and {@link http://logback.qos.ch/manual/mdc.html}
 * @author nullin
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TestLogHelper
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Logger log <span style="color: #339933;">=</span> LoggerFactory.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>TestLogHelper.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> TEST_NAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;testname&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #008000; font-style: italic; font-weight: bold;">/**
   * Adds the test name to MDC so that sift appender can use it and log the new
   * log events to a different file
   * @param name name of the new log file
   * @throws Exception
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> startTestLogging<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
    MDC.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>TEST_NAME, name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #008000; font-style: italic; font-weight: bold;">/**
   * Removes the key (log file name) from MDC
   * @return name of the log file, if one existed in MDC
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> stopTestLogging<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">String</span> name <span style="color: #339933;">=</span> MDC.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>TEST_NAME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    MDC.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span>TEST_NAME<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">return</span> name<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Update test cases</h2>
<p>Now, you just need to make sure that you invoke <tt>TestLogHelper.startTestLogging(String testname)</tt> as early as possible during the test execution. Eventually, by the end of the test you should invoke <tt>TestLogHelper.stopTestLogging()</tt> to ensure that no extra logs get logged into this log file.</p>
<p>As we are using TestNG, for us it was a very simple matter of creating a method annotated @BeforeClass or @BeforeMethod as per requirements and put this code in there. For example, I use the following two methods in the base class for all our tests:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">   @BeforeClass
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testSetUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//start logging to test specific log file</span>
      TestLogHelper.<span style="color: #006633;">startTestLogging</span><span style="color: #009900;">&#40;</span>getTestId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">//</span>
      <span style="color: #666666; font-style: italic;">//Do some setup specific stuff here</span>
      <span style="color: #666666; font-style: italic;">//</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   @AfterClass
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testCleanUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #666666; font-style: italic;">//</span>
         <span style="color: #666666; font-style: italic;">//Do some cleanup specific stuff here</span>
         <span style="color: #666666; font-style: italic;">//</span>
      <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #666666; font-style: italic;">//stop test logging to test specific file</span>
         TestLogHelper.<span style="color: #006633;">stopTestLogging</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getTestId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>testId <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> testId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Configure logback.xml with SiftingAppender</h2>
<p>The last part is to configure the logback.xml to use a SiftingAppender. You can look at the <a  href="http://logback.qos.ch/manual/appenders.html#SiftingAppender">documentation for SiftingAppender</a> to see the different examples. Following is a snippet of the configuration that I am using:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;appender</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;RootSiftAppender&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;ch.qos.logback.classic.sift.SiftingAppender&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;discriminator<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Key<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;strong<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>testname<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/strong<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/Key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;DefaultValue<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;strong<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>testrun<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/strong<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/DefaultValue<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/discriminator<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sift<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;appender</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;FILE-${testname}&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;ch.qos.logback.core.rolling.RollingFileAppender&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;File<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${testname}.log<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/File<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rollingPolicy</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;ch.qos.logback.core.rolling.FixedWindowRollingPolicy&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
               <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;FileNamePattern<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;strong<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${testname}.%i.log<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/strong<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/FileNamePattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
               <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;MinIndex<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/MinIndex<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
               <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;MaxIndex<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>100<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/MaxIndex<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/rollingPolicy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;triggeringPolicy</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
               <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;MaxFileSize<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>5MB<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/MaxFileSize<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/triggeringPolicy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;layout</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;ch.qos.logback.classic.PatternLayout&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
               <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>%d{ISO8601} %-5level %C{1} [%M:%L] [%thread] - %msg%n<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/layout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/appender<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sift<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/appender<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>This configuration says that the test cases would specify the MDC value using a key &#8220;testname&#8221; and if logback finds no such key, it&#8217;ll just log the statements into <tt>testrun.log</tt>.</p>
<p>And that&#8217;s it. You should be good to go.</p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a  href="http://www.nalinmakar.com/2010/03/26/hudson-ci-for-testing/" title="Hudson CI for Testing">Hudson CI for Testing</a></li><li><a  href="http://www.nalinmakar.com/2009/12/23/running-single-test-or-class-using-testng-and-ant/" title="Running single test or class using TestNG and Ant">Running single test or class using TestNG and Ant</a></li><li><a  href="http://www.nalinmakar.com/2008/12/13/connect-to-ms-sql-server-using-jdbc/" title="Connect to MS SQL Server using JDBC">Connect to MS SQL Server using JDBC</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.nalinmakar.com/2010/07/28/logging-tests-to-separate-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Running single test or class using TestNG and Ant</title>
		<link>http://www.nalinmakar.com/2009/12/23/running-single-test-or-class-using-testng-and-ant/</link>
		<comments>http://www.nalinmakar.com/2009/12/23/running-single-test-or-class-using-testng-and-ant/#comments</comments>
		<pubDate>Thu, 24 Dec 2009 01:50:15 +0000</pubDate>
		<dc:creator>Nalin</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[ant]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[test]]></category>
		<category><![CDATA[testng]]></category>

		<guid isPermaLink="false">http://www.nalinmakar.com/?p=656</guid>
		<description><![CDATA[I have been using TestNG for a while now in the latest project that I am working on. We have developed a whole suite of tests for an application and TestNG has really served us well. When it comes to running tests, we find that people (developer, testers and others) usually are hesitant to use [...]]]></description>
			<content:encoded><![CDATA[<p>I have been using <a  href="http://testng.org/doc/index.html">TestNG</a> for a while now in the latest project that I am working on. We have developed a whole suite of tests for an application and TestNG has really served us well.</p>
<p>When it comes to running tests, we find that people (developer, testers and others) usually are hesitant to use something that involves much learning or is different from how they are used to doing things currently. So, we found it very useful to provide utilities that made it easy for anyone to be able to pick up test artifacts and with-in a few steps be able to run tests to reproduce bugs. Because we are building our project using <a  href="http://ant.apache.org/">ant</a>, it was easy to provide a <code>build.xml</code> with the test distribution that will able to run the TestNG tests. This keeps things simple and uniform.</p>
<p>The TestNG ant task uses TestNG suites defined in XML files to run tests. It doesn&#8217;t provide an easy way to run a single test method or all the test methods in a single class. Basically, anytime someone has to run a test using this task, they need to create an XML file specifying the class and method for that test in an XML file and then execute the ant task. This can be simplified by letting ant targets take care of setting up the XML file with the require test and then executing it.</p>
<p><em>It&#8217;s quite simple</em>. You just need to create a TestNG suite XML template, modify it with the parameters that the user passes in on command line and then execute the TestNG ant task. Here&#8217;s how this can be achieved. First, create a template XML file like the following at <code>${testng.templates}/testfn.xml</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;">&lt; !DOCTYPE suite SYSTEM <span style="color: #ff0000;">&quot;http://testng.org/testng-1.0.dtd&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;suite</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Single Method Suite&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;test</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Single Method Test&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;classes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;class</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;@CLASS@&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;methods<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
               <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;exclude</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;.*&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
               <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;include</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;@TEST@&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/methods<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/classes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/test<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/suite<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Now, you can add the following target to the build file:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;run-single-test&quot;</span> </span>
<span style="color: #009900;">         <span style="color: #000066;">description</span>=<span style="color: #ff0000;">&quot;run a specific test. Requires class.name property set to fully qualified name of test class and test.name property set to method name&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;condition</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;propsSpecified&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;and<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;isset</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;class.name&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;isset</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;test.name&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/and<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/condition<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fail</span> <span style="color: #000066;">unless</span>=<span style="color: #ff0000;">&quot;propsSpecified&quot;</span> </span>
<span style="color: #009900;">            <span style="color: #000066;">message</span>=<span style="color: #ff0000;">&quot;class.name and/or test.name property not specified.&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;copy</span> <span style="color: #000066;">todir</span>=<span style="color: #ff0000;">&quot;${tmp.dir}&quot;</span> <span style="color: #000066;">file</span>=<span style="color: #ff0000;">&quot;${testng.templates}/testfn.xml&quot;</span> <span style="color: #000066;">overwrite</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filterset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter</span> <span style="color: #000066;">token</span>=<span style="color: #ff0000;">&quot;CLASS&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;${class.name}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter</span> <span style="color: #000066;">token</span>=<span style="color: #ff0000;">&quot;TEST&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;${test.name}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filterset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/copy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;testng</span> <span style="color: #000066;">classpathref</span>=<span style="color: #ff0000;">&quot;lib.path&quot;</span></span>
<span style="color: #009900;">           <span style="color: #000066;">outputDir</span>=<span style="color: #ff0000;">&quot;${results.dir}/${DSTAMP}.${TSTAMP}-single-test-${class.name}-${test.name}&quot;</span></span>
<span style="color: #009900;">           <span style="color: #000066;">workingDir</span>=<span style="color: #ff0000;">&quot;${results.dir}/${DSTAMP}.${TSTAMP}-single-test-${class.name}-${test.name}&quot;</span></span>
<span style="color: #009900;">           <span style="color: #000066;">verbose</span>=<span style="color: #ff0000;">&quot;2&quot;</span></span>
<span style="color: #009900;">           <span style="color: #000066;">useDefaultListeners</span>=<span style="color: #ff0000;">&quot;false&quot;</span></span>
<span style="color: #009900;">           <span style="color: #000066;">listeners</span>=<span style="color: #ff0000;">&quot;${testng.listeners}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xmlfileset</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${tmp.dir}&quot;</span> <span style="color: #000066;">includes</span>=<span style="color: #ff0000;">&quot;testfn.xml&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jvmarg</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;-Xmx1024m&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/testng<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The target picks the testfn.xml file, replaces the tokens with the specified input and copies it to the temp location. Now, the TestNG task can use this updated XML file to run tests. This target is invoked as:</p>
<p><code>ant run-single-test -Dclass.name=com.nalinmakar.testng.ant.Demo -Dtest.name=Test1</code></p>
<p>for the following class:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.nalinmakar.testng.ant</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Demo
<span style="color: #009900;">&#123;</span>
   @Test
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> Test1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//do something</span>
   <span style="color: #009900;">&#125;</span>
   @Test
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> Test2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//do something</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Similarly, you can also create a template and another ant target for running all the test methods in a class:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;">&lt; !DOCTYPE suite SYSTEM <span style="color: #ff0000;">&quot;http://testng.org/testng-1.0.dtd&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;suite</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Single Class Suite&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;test</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Class Test&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;classes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;class</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;@CLASS@&quot;</span>  <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
       <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/classes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/test<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/suite<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;run-class&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">description</span>=<span style="color: #ff0000;">&quot;run all methods in a specific test class. Requires class.name property to be set to fully qualified name of class&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;condition</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;classNameSpecified&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;isset</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;class.name&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/condition<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fail</span> <span style="color: #000066;">unless</span>=<span style="color: #ff0000;">&quot;classNameSpecified&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">message</span>=<span style="color: #ff0000;">&quot;class.name property not specified. Don't know which test class to run.&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;copy</span> <span style="color: #000066;">todir</span>=<span style="color: #ff0000;">&quot;${tmp.dir}&quot;</span> <span style="color: #000066;">file</span>=<span style="color: #ff0000;">&quot;${testng.templates}/class.xml&quot;</span> <span style="color: #000066;">overwrite</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filterset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter</span> <span style="color: #000066;">token</span>=<span style="color: #ff0000;">&quot;CLASS&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;${class.name}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filterset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/copy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;testng</span> <span style="color: #000066;">classpathref</span>=<span style="color: #ff0000;">&quot;lib.path&quot;</span></span>
<span style="color: #009900;">           <span style="color: #000066;">outputDir</span>=<span style="color: #ff0000;">&quot;${results.dir}/${DSTAMP}.${TSTAMP}-class&quot;</span></span>
<span style="color: #009900;">           <span style="color: #000066;">workingDir</span>=<span style="color: #ff0000;">&quot;${results.dir}/${DSTAMP}.${TSTAMP}-class&quot;</span></span>
<span style="color: #009900;">           <span style="color: #000066;">verbose</span>=<span style="color: #ff0000;">&quot;2&quot;</span></span>
<span style="color: #009900;">           <span style="color: #000066;">useDefaultListeners</span>=<span style="color: #ff0000;">&quot;false&quot;</span></span>
<span style="color: #009900;">           <span style="color: #000066;">listeners</span>=<span style="color: #ff0000;">&quot;${testng.listeners}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;jvmarg</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;-Xmx1024m&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xmlfileset</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${tmp.dir}&quot;</span> <span style="color: #000066;">includes</span>=<span style="color: #ff0000;">&quot;class.xml&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/testng<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>and can invoke this as </p>
<p><code>ant run-single-test -Dclass.name=com.nalinmakar.testng.ant.Demo</code></p>
<h3  class="related_post_title">Related Posts</h3><ul class="related_post"><li><a  href="http://www.nalinmakar.com/2010/07/28/logging-tests-to-separate-files/" title="Logging tests to separate files">Logging tests to separate files</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.nalinmakar.com/2009/12/23/running-single-test-or-class-using-testng-and-ant/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
