You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by bo...@apache.org on 2004/08/20 12:55:20 UTC

cvs commit: ant/proposal/sandbox/dotnet/src/testcases/org/apache/tools/ant/taskdefs/optional/dotnet NUnitTaskTest.java

bodewig     2004/08/20 03:55:20

  Modified:    proposal/sandbox/dotnet README
               proposal/sandbox/dotnet/docs index.html
               proposal/sandbox/dotnet/src/main/org/apache/tools/ant/taskdefs/optional/dotnet
                        antlib.xml
  Added:       proposal/sandbox/dotnet/docs nunit.html
               proposal/sandbox/dotnet/src/etc/testcases nunit.xml
               proposal/sandbox/dotnet/src/etc/testcases/src fail.cs
                        pass.cs
               proposal/sandbox/dotnet/src/main/org/apache/tools/ant/taskdefs/optional/dotnet
                        NUnitTask.java
               proposal/sandbox/dotnet/src/testcases/org/apache/tools/ant/taskdefs/optional/dotnet
                        NUnitTaskTest.java
  Log:
  <nunit>
  
  Revision  Changes    Path
  1.3       +0 -2      ant/proposal/sandbox/dotnet/README
  
  Index: README
  ===================================================================
  RCS file: /home/cvs/ant/proposal/sandbox/dotnet/README,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- README	16 Apr 2004 09:12:43 -0000	1.2
  +++ README	20 Aug 2004 10:55:20 -0000	1.3
  @@ -32,8 +32,6 @@
   
   * A <wix> task.
   
  -Mid term goals:
  -
   * A <nunit> task.
   
   Those tasks should end up in an antlib of their own in order to be
  
  
  
  1.5       +4 -0      ant/proposal/sandbox/dotnet/docs/index.html
  
  Index: index.html
  ===================================================================
  RCS file: /home/cvs/ant/proposal/sandbox/dotnet/docs/index.html,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- index.html	2 Jul 2004 11:46:09 -0000	1.4
  +++ index.html	20 Aug 2004 10:55:20 -0000	1.5
  @@ -112,6 +112,10 @@
         tool, untested.</li>
   
         <li><a href="wix.html">wix</a> - execute the WiX toolset, untested.</li>
  +
  +      <li><a href="nunit.html">nunit</a> - execute the
  +      nunit-console.exe <a href="http://www.nunit.org/">NUnit</a>
  +      test runner.</li>
       </ul>
   
       <hr/>
  
  
  
  1.1                  ant/proposal/sandbox/dotnet/docs/nunit.html
  
  Index: nunit.html
  ===================================================================
  <html>
    <head>
      <meta http-equiv="Content-Language" content="en-us"></meta>
      <title>NUnit Task</title>
    </head>
  
    <body>
      <h2><a name="dotnetexec">NUnit</a></h2>
  
      <h3>Description</h3>
  
      <p>Runs the <a href="http://www.nunit.org/">NUnit</a> console
      test runner.</p>
  
      <h3>Parameters</h3>
      <table border="1" cellpadding="2" cellspacing="0">
        <tr>
          <td valign="top"><b>Attribute</b></td>
          <td valign="top"><b>Description</b></td>
          <td align="center" valign="top"><b>Required</b></td>
        </tr>
        <tr>
          <td valign="top">vm</td>
          <td valign="top">Same as <a
              href="dotnetexec.html">dotnetexec</a>'s vm attribute.
            Specify the framework to use.</td>
          <td align="center">No.</td>
        </tr>
        <tr>
          <td valign="top">config</td>
          <td valign="top">Config file to use</td>
          <td align="center">No.</td>
        </tr>
        <tr>
          <td valign="top">output</td>
          <td valign="top">Where test output should go.</td>
          <td align="center">No.</td>
        </tr>
        <tr>
          <td valign="top">error</td>
          <td valign="top">Where test error output should go.</td>
          <td align="center">No.</td>
        </tr>
        <tr>
          <td valign="top">xmlout</td>
          <td valign="top">Where NUnit's XML output should go.</td>
          <td align="center">No.</td>
        </tr>
        <tr>
          <td valign="top">transform</td>
          <td valign="top">The transformation to apply.</td>
          <td align="center">No.</td>
        </tr>
        <tr>
          <td valign="top">thread</td>
          <td valign="top">Causes a separate thread to be created for
          running the tests - see the NUnit documentation for
          details.</td>
          <td align="center">No - defaults to false.</td>
        </tr>
        <tr>
          <td valign="top">noshadow</td>
          <td valign="top">Disables shadow copying of the assembly in
          order to provide improved performance..</td>
          <td align="center">No - defaults to false.</td>
        </tr>
        <tr>
          <td valign="top">fixture</td>
          <td valign="top">Test fixture to run.</td>
          <td align="center">No.</td>
        </tr>
        <tr>
          <td valign="top">labels</td>
          <td valign="top">Causes an identifying label to be displayed
          at the start of each test case.</td>
          <td align="center">No - defaults to false.</td>
        </tr>
        <tr>
          <td valign="top">failOnError</td>
          <td valign="top">Stops the build if NUnit returns with a code
          indicating an error or failure.</td>
          <td align="center">No - defaults to false.</td>
        </tr>
      </table>
  
      <h3>Parameters specified as nested elements</h3>
      <h4>testassembly</h4>
  
      <p><code>testassembly</code> has a single required attribute name -
      specifies an assembly or a project file holding tests.</p>
  
      <h4>include/exclude</h4>
  
      <p><code>in/exclude</code> have a single required attribute name -
      specifies a category to include or exclude from the tests.</p>
  
      <p><b>Note</b> that in current versions of NUnit (up to 2.2) you
      may choose to either include or exclude categories in a run, but
      not both.  This task does <b>not</b> enforce this restriction
      since future versions of NUnit may change behavior.</p>
  
      <h4>redirector</h4>
  
      <p>A nested <a
      href="http://ant.apache.org/manual/CoreTypes/redirector.html">I/O
      Redirector</a> can be specified.  Any file mapping is done using a
      <code>null</code> sourcefile; therefore not all <a
      href="http://ant.apache.org/manual/CoreTypes/mapper.html">Mapper</a>
      types will return results.</p>
  
      <h4>env</h4> <p>It is possible to specify environment variables to
      pass to the system command via nested <code>&lt;env&gt;</code>
      elements.  They support the same attributes as the nested <a
      href="http://ant.apache.org/manual/CoreTasks/exec.html#env"><code>env</code>
      element of the <code>&lt;exec&gt;</code> task</a>.</p>
  
      <h3>Examples</h3>
  
      <h4>Specify an assembly or project:</h4>
  
      <pre>
        &lt;nunit&gt;
          &lt;testassembly name="nunit.tests.dll"/&gt;
        &lt;/nunit&gt;
      </pre>
      
      <p>or</p>
  
      <pre>
        &lt;nunit&gt;
          &lt;testassembly name="nunit.tests.csproj"/&gt;
        &lt;/nunit&gt;
      </pre>
      
      <h4>Specifying an Assembly and a Fixture</h4>
  
      <pre>
        &lt;nunit fixture="NUnit.Tests.AssertionTests"&gt;
          &lt;testassembly name="nunit.tests.dll"/&gt;
        &lt;/nunit&gt;
      </pre>
  
      <h4>Specifying Test Categories to Include</h4>
  
      <pre>
        &lt;nunit&gt;
          &lt;testassembly name="nunit.tests.dll"/&gt;
          &lt;include name="Baseline"/&gt;
        &lt;/nunit&gt;
      </pre>
  
      <h4>Specifying the XML file name</h4>
  
      <pre>
        &lt;nunit xmlout="console-test.xml"&gt;
          &lt;testassembly name="nunit.tests.dll"/&gt;
        &lt;/nunit&gt;
      </pre>
  
      <p>changes the name of the output file to "console-test.xml" -
      note that this task will resolve file names relative to the
      project's base directory, not the current working directory.</p>
  
      <h4>Specifying Multiple Assemblies</h4>
  
      <pre>
        &lt;nunit&gt;
          &lt;testassembly name="assembly1.dll"/&gt;
          &lt;testassembly name="assembly2.dll"/&gt;
          &lt;testassembly name="assembly3.dll"/&gt;
        &lt;/nunit&gt;
      </pre>
  
      <hr/>
        <p align="center">Copyright &copy; 2004 The Apache Software Foundation. All rights Reserved.</p>
    </body>
  </html>
  
  
  1.1                  ant/proposal/sandbox/dotnet/src/etc/testcases/nunit.xml
  
  Index: nunit.xml
  ===================================================================
  <?xml version="1.0"?>
  <!--
   Copyright  2004 The Apache Software Foundation
  
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
  
        http://www.apache.org/licenses/LICENSE-2.0
  
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
  -->
  <project name="nunit" basedir="." default="echo"
    xmlns:dn="antlib:org.apache.tools.ant.taskdefs.optional.dotnet">
  
    <property name="build.dir" value="build"/>
    <property name="src.dir" location="src"/>
  
    <taskdef 
      uri="antlib:org.apache.tools.ant.taskdefs.optional.dotnet"
      resource="org/apache/tools/ant/taskdefs/optional/dotnet/antlib.xml">
      <classpath>
        <pathelement location="../../../build/lib/dotnet.jar"/>
      </classpath>
    </taskdef>
  
    <property environment="env"/>
    <condition property="nunit.found">
      <or>
        <available file="nunit-console.exe" filepath="${env.PATH}"/>
        <available file="nunit-console.exe" filepath="${env.Path}"/>
        <available file="nunit-console.exe"/>
      </or>
    </condition>
  
    <target name="no-assembly">
      <dn:nunit/>
    </target>
  
    <target name="compile-pass">
      <mkdir dir="${build.dir}"/>
      <csc destFile="${build.dir}/Pass.dll"
        targetType="library" references="nunit.framework.dll">
        <src dir="${src.dir}" includes="pass.cs"/>
      </csc>
    </target>
  
    <target name="compile-fail">
      <mkdir dir="${build.dir}"/>
      <csc destFile="${build.dir}/Fail.dll" 
        targetType="library" references="nunit.framework.dll">
        <src dir="${src.dir}" includes="fail.cs"/>
      </csc>
    </target>
  
    <target name="passing-test" depends="compile-pass">
      <dn:nunit>
        <testassembly name="${build.dir}/Pass.dll"/>
      </dn:nunit>
    </target>
  
    <target name="failing-test" depends="compile-fail">
      <dn:nunit>
        <testassembly name="${build.dir}/Fail.dll"/>
      </dn:nunit>
    </target>
  
    <target name="failing-test-with-fail" depends="compile-fail">
      <dn:nunit failonerror="true">
        <testassembly name="${build.dir}/Fail.dll"/>
      </dn:nunit>
    </target>
  
    <target name="teardown">
      <delete dir="${build.dir}"/>
    </target>
  </project>
  
  
  1.1                  ant/proposal/sandbox/dotnet/src/etc/testcases/src/fail.cs
  
  Index: fail.cs
  ===================================================================
  /*
   * Copyright  2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   *
   */
  
  using System;
  using NUnit.Framework;
  
  [TestFixture]
  public class FailingTest
  {
  
      [Test]
      public void Fail() 
      {
          Assert.IsTrue(false);
      }
  }
  
  
  
  1.1                  ant/proposal/sandbox/dotnet/src/etc/testcases/src/pass.cs
  
  Index: pass.cs
  ===================================================================
  /*
   * Copyright  2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   *
   */
  
  using System;
  using NUnit.Framework;
  
  [TestFixture]
  public class PassingTest
  {
  
      [Test]
      public void Pass() 
      {
          Assert.IsTrue(true);
      }
  }
  
  
  
  1.5       +4 -0      ant/proposal/sandbox/dotnet/src/main/org/apache/tools/ant/taskdefs/optional/dotnet/antlib.xml
  
  Index: antlib.xml
  ===================================================================
  RCS file: /home/cvs/ant/proposal/sandbox/dotnet/src/main/org/apache/tools/ant/taskdefs/optional/dotnet/antlib.xml,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- antlib.xml	16 Apr 2004 14:17:58 -0000	1.4
  +++ antlib.xml	20 Aug 2004 10:55:20 -0000	1.5
  @@ -31,4 +31,8 @@
       name="wix"
       classname="org.apache.tools.ant.taskdefs.optional.dotnet.WixTask"
       />
  +  <taskdef
  +    name="nunit"
  +    classname="org.apache.tools.ant.taskdefs.optional.dotnet.NUnitTask"
  +    />
   </antlib>
  
  
  
  1.1                  ant/proposal/sandbox/dotnet/src/main/org/apache/tools/ant/taskdefs/optional/dotnet/NUnitTask.java
  
  Index: NUnitTask.java
  ===================================================================
  /*
   * Copyright  2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   *
   */
  
  package org.apache.tools.ant.taskdefs.optional.dotnet;
  
  import org.apache.tools.ant.BuildException;
  import org.apache.tools.ant.Task;
  import org.apache.tools.ant.types.Environment;
  import org.apache.tools.ant.types.RedirectorElement;
  
  import java.io.File;
  import java.util.ArrayList;
  import java.util.Iterator;
  
  /**
   * Task to run the NUnit Console test runner.
   *
   * @see http://www.nunit.org/
   */
  public class NUnitTask extends Task {
  
      /**
       * The vm attribute - if given.
       */
      private String vm;
  
      /**
       * Test assemblies.
       */
      private ArrayList testAssemblies = new ArrayList();
  
      /**
       * The /config argument.
       */
      private File configFile;
  
      /**
       * The /output argument.
       */
      private File out;
  
      /**
       * The /err argument.
       */
      private File err;
  
      /**
       * The /xml argument.
       */
      private File xmlOut;
  
      /**
       * The /transform argument.
       */
      private File transform;
  
      /**
       * The /thread argument.
       */
      private boolean thread = false;
  
      /**
       * The /fixture argument.
       */
      private String fixture;
  
      /**
       * Categories to include.
       */
      private ArrayList includes = new ArrayList();
  
      /**
       * Categories to exclude.
       */
      private ArrayList excludes = new ArrayList();
  
      /**
       * The /noshadow argument.
       */
      private boolean noshadow = false;
  
      /**
       * The /labels argument.
       */
      private boolean labels = false;
  
      /**
       * Redirects everything that NUnit wants to send to the console.
       */
      private RedirectorElement redirectorElement;
  
      /**
       * Whether a failure should stop the build.
       */
      private boolean failOnError = false;
  
      /**
       * Support for nested environment variables.
       */
      private Environment env = new Environment();
  
      public NUnitTask() {
          super();
      }
  
      /**
       * Set the name of the executable for the virtual machine.
       *
       * @param value the name of the executable for the virtual machine
       */
      public void setVm(String value) {
          this.vm = value;
      }
  
      /**
       * Sets the name of the config file.
       */
      public void setConfig(File c) {
          configFile = c;
      }
  
      /**
       * The /output argument.
       */
      public void setOut(File out) {
          this.out = out;
      }
  
      /**
       * The /err argument.
       */
      public void setError(File err) {
          this.err = err;
      }
  
      /**
       * The /xml argument.
       */
      public void setXmlOut(File out) {
          this.xmlOut = out;
      }
  
      /**
       * The /transform argument.
       */
      public void setTransform(File transform) {
          this.transform = transform;
      }
  
      /**
       * The /thread argument.
       */
      public void setThread(boolean thread) {
          this.thread = thread;
      }
  
      /**
       * The /fixture argument.
       */
      public void setFixture(String fixture) {
          this.fixture = fixture;
      }
  
      /**
       * The /noshadow argument.
       */
      public void setNoshadow(boolean noshadow) {
          this.noshadow = noshadow;
      }
  
      /**
       * The /labels argument.
       */
      public void setLabels(boolean labels) {
          this.labels = labels;
      }
  
      /**
       * Whether a failure should stop the build.
       */
      public void setFailOnError(boolean b) {
          failOnError = b;
      }
  
      /**
       * Adds a test assembly by name.
       */
      public void addTestAssembly(NamedElement a) {
          testAssemblies.add(a);
      }
  
      /**
       * Adds a category to the include list.
       */
      public void addInclude(NamedElement a) {
          includes.add(a);
      }
  
      /**
       * Adds a category to the exclude list.
       */
      public void addExclude(NamedElement a) {
          excludes.add(a);
      }
  
      /**
       * Add an environment variable to the launched process.
       *
       * @param var new environment variable
       */
      public void addEnv(Environment.Variable var) {
          env.addVariable(var);
      }
  
      /**
       * Add a <code>RedirectorElement</code> to this task.
       *
       * <p>This does not use the <code>out</code> and
       * <code>error</code> attributes, it only captures NUnits output
       * that has not been redirected by those attributes.</p>
       */
      public void addConfiguredRedirector(RedirectorElement redirectorElement) {
          if (this.redirectorElement != null) {
              throw new BuildException("cannot have > 1 nested <redirector>s");
          } else {
              this.redirectorElement = redirectorElement;
          }
      }
  
      public void execute() {
          if (testAssemblies.size() == 0) {
              throw new BuildException("You must specify at least one test "
                                       + "assembly.");
          }
          
          DotNetExecTask exec = DotNetExecTask.getTask(this, vm, 
                                                       "nunit-console.exe",
                                                       env);
          Iterator iter = testAssemblies.iterator();
          while (iter.hasNext()) {
              NamedElement a = (NamedElement) iter.next();
              exec.createArg().setValue(a.getName());
          }
          if (configFile != null) {
              exec.createArg().setValue("/config=" 
                                        + configFile.getAbsolutePath());
          }
          exec.createArg().setValue("/nologo");
  
          if (out != null) {
              exec.createArg().setValue("/output=" + out.getAbsolutePath());
          }
          if (err != null) {
              exec.createArg().setValue("/err=" + err.getAbsolutePath());
          }
          if (xmlOut != null) {
              exec.createArg().setValue("/xml=" + xmlOut.getAbsolutePath());
          }
          if (transform != null) {
              exec.createArg().setValue("/transform=" 
                                        + transform.getAbsolutePath());
          }
  
          if (thread) {
              exec.createArg().setValue("/thread");
          }
          if (noshadow) {
              exec.createArg().setValue("/noshadow");
          }
          if (labels) {
              exec.createArg().setValue("/labels");
          }
          if (fixture != null) {
              exec.createArg().setValue("/fixture=" + fixture);
          }
          
          if (includes.size() > 0) {
              StringBuffer sb = new StringBuffer("/include=");
              iter = includes.iterator();
              boolean first = false;
              while (iter.hasNext()) {
                  if (first) {
                      first = false;
                  } else {
                      sb.append(",");
                  }
                  NamedElement a = (NamedElement) iter.next();
                  sb.append(a.getName());
              }
              exec.createArg().setValue(sb.toString());
          }
          if (excludes.size() > 0) {
              StringBuffer sb = new StringBuffer("/exclude=");
              iter = excludes.iterator();
              boolean first = false;
              while (iter.hasNext()) {
                  if (first) {
                      first = false;
                  } else {
                      sb.append(",");
                  }
                  NamedElement a = (NamedElement) iter.next();
                  sb.append(a.getName());
              }
              exec.createArg().setValue(sb.toString());
          }
  
          if (redirectorElement != null) {
              exec.addConfiguredRedirector(redirectorElement);
          }
          exec.setFailonerror(failOnError);
  
          exec.execute();
      }
  
      public static class NamedElement {
          private String name;
          public String getName() {return name;}
          public void setName(String s) {name = s;}
      }
  }
  
  
  1.1                  ant/proposal/sandbox/dotnet/src/testcases/org/apache/tools/ant/taskdefs/optional/dotnet/NUnitTaskTest.java
  
  Index: NUnitTaskTest.java
  ===================================================================
  /*
   * Copyright  2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   *
   */
  package org.apache.tools.ant.taskdefs.optional.dotnet;
  
  import org.apache.tools.ant.BuildFileTest;
  
  /**
   * Tests the NUnitTask task.
   */
  public class NUnitTaskTest extends BuildFileTest {
  
      /**
       * Description of the Field
       */
      private final static String TASKDEFS_DIR = "src/etc/testcases/";
  
      /**
       * Constructor 
       *
       * @param name testname
       */
      public NUnitTaskTest(String name) {
          super(name);
      }
  
      /**
       * The JUnit setup method
       */
      public void setUp() {
          configureProject(TASKDEFS_DIR + "nunit.xml");
      }
  
      /**
       * The teardown method for JUnit
       */
      public void tearDown() {
          executeTarget("teardown");
      }
  
      public void testNoAssembly() {
          expectSpecificBuildException("no-assembly", "no assembly", 
                                       "You must specify at least one test assembly.");
      }
  
      public void testPass() {
          if (getProject().getProperty("nunit.found") != null) {
              expectLogContaining("passing-test", 
                                  "Tests run: 1, Failures: 0, Not run: 0");
          }
      }
  
      public void testFail() {
          if (getProject().getProperty("nunit.found") != null) {
              expectLogContaining("failing-test", 
                                  "Tests run: 1, Failures: 1, Not run: 0");
          }
      }
  
      public void testFailOnFail() {
          if (getProject().getProperty("nunit.found") != null) {
              expectBuildException("failing-test-with-fail", "test should fail");
          }
      }
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org