wiki:ComTestCsharp

Version 40 (modified by anlakane, 8 years ago) (diff)

--

ComTest for C#

  • In early alpha stage.
  • Uses Visual Studio 2010 built-in test system --> Requires Professional, Premium or Ultimate version of VS
  • Java must be installed before using ComTest.
    • Checking on Windows: Go to Control Panel -> Programs and Features, there should be Java(TM) SE Development Kit 6 (or some other version of Java SDK)

Installation

ComTest C#

Copy ComTest.jar somewhere. Note down this path, for you will need it when you are configuring the Visual Studio plugin.

Visual Studio 2010 plugin

First make sure you have Visual Studio 2010 installed with C# language support. The plugin does not work with previous versions of Visual Studio or with other languages such as Visual Basic or Visual C++.

Download the plugin installer from here and run it. Follow the installer's instructions. If the installation was successful, you should see a new ComTest item in the Tools menu when a C# project is open. If you have older version installed, uninstall it from Tools/Extension Manager/All.

From revision 280, the plugin no longer needs the CLASSPATH environmental variable pointing to the executable. Instead there is now a ComTest Options item in the Tools menu that opens a window where the path can be configured. You can also use a different Java interpreter if the one in your path is not working for you. To the "Path to ComTest.jar executable" write the full path including the .jar name, f.ex:

e:\MyBin\ComText.jar

Please do not use directory or filenames that includes spaces.

Visual Studio 2010 code snippet

This optional component makes creating new tests in Visual Studio easier. First try:

  1. Download it from here, run it and follow the installer's instructions. If you get a warning about no signature found, you can skip it safely.

If the installer does not work:

  1. save the ComTest.snippet to the directory (press mouse right button over the link and choose "save to disk" or whatever it is in different browers)
    ...\My Documents\Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets        
    

Restart Visual Studio after installation

After you have the snippet installed, you can create a test by writing "comt" and pressing TAB 2 times when the cursor is after an XML comment.

/// <summary>
/// Multiplies to numbers
/// </summary>
/// <param name="a">first number</param>
/// <param name="b">second number</param>
/// <returns>a*b</returns>
comt|
public static double Multiply( double a, double b )

After pressing TAB two times the code looks like this:

/// <summary>
/// Multiplies to numbers
/// </summary>
/// <param name="a">first number</param>
/// <param name="b">second number</param>
/// <returns>a*b</returns>
/// <example>
/// <pre name="test">
/// |
/// </pre>
/// </example>
public static double Multiply( double a, double b )

Usage with the Visual Studio 2010 plugin

This is the preferred way of usage. Just select ComTest from the Tools menu, or press Ctrl+Shit+Q to run ComTest. After a while, a test project should appear in Project Explorer window. The tests are also run automatically, and the results should appear in a window at the bottom part of the Visual Studio IDE.

Usage from command line

ComTest-C# accepts C# .cs files and Visual Studio projects (.csproj) and solutions (.sln), so that

  • test code files are generated from individual source files (ProgramTest.cs from Program.cs)
  • test project files are generated from project files (Project1Test.csproj from Project1.csproj) and all containing code files are processed
  • test solution files are MODIFIED so that all generated test project files are included. All containing non-test C# projects are processed. This is what the Visual Studio plugin does. The original solution file is stored in {SolutionName.sln.ctbackup}.

If you have ComTest.jar in your classpath, you can run it with

java comtest.ComTest file1.cs file2.cs file3.cs

If not, run

java -jar path\to\ComTest.jar file1.cs file2.cs file3.cs

The content of a file is determined by its extension. For instructions how to override this and other advanced options, run ComTest with no parameters.

java comtest.ComTest

or

java -jar path\to\ComTest.jar

Writing tests

The tests are written in a comment block before a method to be tested. Different commenting styles can be used.

/// This is a XML comment block.
/// Use XML comments if you want to document something.
/// Doxygen can read XML comments.
/// Generally, these are the ones you want to use with tests.
/**
 * This is a Javadoc comment block.
 * These are used to comment Java code, but not so much in C#.
 * Doxygen can read these, too.
 */
// This is a C++ comment block.
// If you want to create tests and not include them in documentation,
// use these comments.
/*
 * This is a C comment block.
 * Notice the single star on the first line.
 * These comments are not used for documentation, either.
 */

The start of the test block is signified by

<example>
<pre name="test">

After that comes the test code. The test block is terminated by

</pre>
</example>

You can find more information about the syntax of ComTest macro language here.

Example test

public class Matrix
{
    /// <summary>
    /// Find what line of matrix contains the search value
    /// </summary>
    /// <param name="mat">Matrix where we search</param>
    /// <param name="value">value we search</param>
    /// <returns>row index where we found the value.
    /// If many lines contains the value, we return the first index.
    /// If the matrix does not contain value, return -1</returns>
    /// <example>
    /// <pre name="test">
    /// int[,] m = {{2,4,1}, {9,2,0}, {5,6,1}, {0,12,3}};
    /// Matrix.WhatRowContains(m, 1) === 0;
    /// Matrix.WhatRowContains(m, 0) === 1;
    /// Matrix.WhatRowContains(m, 12) === 3;
    /// Matrix.WhatRowContains(m, 11) === -1;
    /// </pre>
    /// </example>
    public static int Matrix.WhatRowContains(int[,] mat, int value)
    {
        for (int iy = 0; iy < mat.GetLength(0); iy++)
            for (int ix = 0; ix < mat.GetLength(1); ix++)
            {
                int luku = mat[iy, ix];
                if (luku == value) return iy;
            }
        return -1;
    }
}

Know problems

  • does not work with Visual Studio Express (because NUnit is not supported)
  • does not work on mounted or substed drives. The project must be in real disk in the computer.
  • sometimes the Java-path must be given with full path like:
    "c:\Program Files (x86)\Java\jre6\bin\java.exe"
    

Attachments