Ryan Harrison My blog, portfolio and technology related ramblings

C# - The basis of a unit testing framework

Cut out all the sugar from pretty much any unit testing framework and you have yourself the situation where the users have marked their test classes to signify that they contain test methods and have marked the test methods themselves to tell the framework to actually run them.

So really on the base level (cutting out assertions and such) you need to somehow figure out which methods in which classes should be run by your tester. In managed languages this becomes a lot simpler through the use of reflection. We can simply ‘reflect’ on each of the classes/methods and decide if they should be run or not.

Here is some short C# code that demonstrates this idea through attributes and reflection. Classes which contain test methods are marked with the TestClass attribute and the test methods themselves which will be run later on are marked with the TestMethod attribute. The code uses LINQ to first get all the types in the currently executing assembly which include the TestClass attribute. Then all the methods are found in each of those classes which are marked with the TestMethod attribute. Finally each of those test methods are invoked using a new instance of the test class itself. The code is all commented so it should all be self-explanatory.

This simple idea can then be added onto in a load of different ways. For example some methods are expected to throw certain exceptions. The TestMethod attribute could therefore be modified to allow that exception through. The unit tester itself could then be adapted to catch that exception and still pass.

  
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Reflection;

namespace UnitTester  
{  
    // Attribute to mark that a class contains tests that should be run  
    [AttributeUsage(AttributeTargets.Class)]  
    internal class TestClassAttribute : Attribute  
    {  
    }

    // Attribute to mark that a method is a test and so should be run  
    [AttributeUsage(AttributeTargets.Method)]  
    internal class TestMethodAttribute : Attribute  
    {  
    }

    // Class does not have TestClassAttribute so will ignored  
    internal class TestSuiteOne  
    {  
        // Method has TestMethod attribute but resides in a class without TestClass so will be ignored  
        [TestMethod]  
        public void DoTestOne()  
        {  
            Console.WriteLine("Doing test one in TestSuiteOne");  
        }  
    }

    // Class does have TestClassAttribute so will not be ignored  
    [TestClass]  
    internal class TestSuiteTwo  
    {  
        // Helper method does not have TestMethod attribute so will be ignored by the tester  
        // However will be run once indirectly through DoTestOne()  
        private void Helper()  
        {  
            Console.WriteLine("Helping to run the tests in TestSuiteTwo");  
        }

        // Method has TestMethod attribute so will be run by the tester  
        [TestMethod]  
        public void DoTestOne()  
        {  
            Console.WriteLine("Doing test one in TestSuiteTwo");  
            Helper();  
        }

        // Method has TestMethod attribute so will be run by the tester  
        [TestMethod]  
        public void DoTestTwo()  
        {  
            Console.WriteLine("Doing test two in TestSuiteTwo");  
        }  
    }

    internal class Program  
    {  
        public static void Main()  
        {  
            // Get access to all the types in the executing assembly  
            Assembly assembly = Assembly.GetExecutingAssembly();

            // Get all the types whose custom attributes includes TestClassAttribute  
            IEnumerable<Type> testClasses =  
            assembly.GetTypes().Where(t => t.GetCustomAttributes(false).Any(a => a is TestClassAttribute));

            foreach (Type testClass in testClasses)  
            {  
                Console.WriteLine("Running tests in TestClass: " + testClass.Name);

                // Get all the methods from the testclass whose custom attributes includes TestMethodAttribute  
                IEnumerable<MethodInfo> testMethods =  
                testClass.GetMethods().Where(m => m.GetCustomAttributes(false).Any(a => a is TestMethodAttribute));

                // Create an instance of the test class so we can run the test methods from it  
                object testClassInstance = Activator.CreateInstance(testClass);  
                foreach (MethodInfo testMethod in testMethods)  
                {  
                    // Run each of the test methods using the instance of the testclass  
                    testMethod.Invoke(testClassInstance, null);  
                }  
            }  
        }  
    }  
}  
Read More

Java - Write code indirectly using reflection

In a recent post I showed how it is possible to write all your C# code indirectly through reflection. Here is the same example in Java. The process itself is very much the same - retrieve meta information about the classes/methods we are interested in and then invoke them passing in the object it would normally be called from. The classes themselves are all very similar through C# to Java. Type becomes Class, MethodInfo becomes Method etc.

A more interesting difference is how the Java version uses variable length parameters in the .invoke calls on Method objects as a way of passing parameters to the particular method. In the C# version you have to explicitly pass a Type or object array. I think the Java version is slightly more user friendly in that quite small regard even though the actual parameter itself is an array in both circumstances.

Its also interesting to see just how many exceptions the Java version can throw in the process. Because they are all checked you are forced to deal with them whereas C# does not have this feature. Its probably likely then that you would have to catch a more generic exception in the C# version.

Here is the code for the Java version. The result is the same as the C# version.

  
import java.lang.reflect.InvocationTargetException;  
import java.lang.reflect.Method;

public class Reflector {

    public static void main(String[] args) throws InstantiationException, IllegalAccessException, SecurityException,  
        NoSuchMethodException, IllegalArgumentException, InvocationTargetException {

        // Normal method  
        System.out.println("Normal method:\n");  
        Foo foo = new Foo();  
        foo.setFirst(36);  
        foo.setSecond(83);  
        System.out.println(foo.toString());

        System.out.println("\nUsing reflection:\n");  
        Class<?> fooClass = Foo.class;

        Foo fooInstance = (Foo) fooClass.newInstance();  
        Method setFirstMethod = fooClass.getMethod("setFirst", int.class);  
        Method setSecondMethod = fooClass.getMethod("setSecond", int.class);  
        setFirstMethod.invoke(fooInstance, 36);  
        setSecondMethod.invoke(fooInstance, 83);

        Method toString = fooClass.getMethod("toString", null);  
        String result = (String) toString.invoke(fooInstance, null);

        Method println = System.out.getClass().getMethod("println", String.class);  
        println.invoke(System.out, result);
    }
}

class Foo {

    private int first;  
    private int second;

    public void setFirst(int newFirst) {  
        this.first = newFirst;  
    }

    public void setSecond(int newSecond) {  
        this.second = newSecond;  
    }

    @Override  
    public String toString() {  
        return "First = " + first + ", Second = " + second;  
    }  
}  

Output:

  
Normal method:

First = 36, Second = 83

Using reflection:

First = 36, Second = 83  

Here is the C# version.

Read More

Java - Snippet Manager

The source code for my Java swing Snippet Manager application is now on GitHub.

Download the JAR file

Main Features -

  • A centralised repository of all your small code snippets and memos
  • Create, edit and origanise a selection of code, or plain text, snippets with syntax highlighting
  • Supported languages include C#, Java, PHP and Pascal
  • Assign keywords and descriptions to each snippet and use the search field to filter out your current set of snippets accordingly
  • Save your snippet collection to an XML file for use on any computer. Have separate files for different snippet sets
  • Easy and to use intuitive interface with automatic line numberings, syntax highlighting and find/replace dialog

Snippet Manager Screenshot

Full source code

Download an exectuable JAR file

Read More

C# - Write code indirectly using reflection

You can use the reflection features in C# to write your normal code in an indirect manner. No idea why you would want to do something like this, but it gives an idea of the control you get when using reflection.

In this example we have a small class wrapping two integers inside their own properties. First of all an object is created, the properties set, and the .ToString() method result passed into a Console.WriteLine using the conventional way. The exact same thing happens again but now indirectly through the use of reflection. As you can see it takes up nearly three times more code to to the exact same thing using reflection but you at least get the same result.

  
class Program  
{  
    static void Main()
    {  
        Console.WriteLine("Using normal method:\n");

        // Normal method of instantiation and calling methods  
        Foo obj = new Foo();  
        obj.First = 36;  
        obj.Second = 83;  
        Console.WriteLine(obj.ToString());

        Console.WriteLine("\nUsing reflection:\n");  
        Type fooType = typeof (Foo);

        // Get an instance of the class  
        Foo fooInstance = (Foo) Activator.CreateInstance(fooType);

        // Get and set values of the two int properties  
        PropertyInfo firstPropInfo = fooType.GetProperty("First", typeof(int));  
        PropertyInfo secondPropInfo = fooType.GetProperty("Second", typeof(int));

        firstPropInfo.SetValue(fooInstance, 36, null);  
        secondPropInfo.SetValue(fooInstance, 83, null);

        // Run the ToString method on the object  
        MethodInfo toStringInfo = fooType.GetMethod("ToString");  
        string toStringResult = (string) toStringInfo.Invoke(fooInstance, null);

        // Run Console.WriteLine using the ToString result  
        MethodInfo writeLineInfo = typeof (Console).GetMethod("WriteLine", new[] {typeof(string)});  
        writeLineInfo.Invoke(null, new[] {toStringResult });  
    }  
}

class Foo  
{  
    public int First { get; set; }  
    public int Second { get; set; }

    public override string ToString()  
    {  
        return "First = " + First + ", Second = " + Second;  
    }  
}  

Output:

  
Using normal method:

First = 36, Second = 83

Using reflection:

First = 36, Second = 83  

Here is a Java version of the same example

Read More

Numerical Methods Helper

The full source code for my numerical methods helper is now available on the GitHub repository.

Download the User Manual

Download the executable

Numerical Methods Helper is a learning tool for numerical algorithms in AS and A2 Mathematics modules. The program supports methods of numerical integration, root finding through iteration, local extrema, and tangents and normal lines to functions.

The progress of each algorithm is drawn onto a freely movable and interactive graph to allow students the most engaging learning experience. All parameters for each algorithm are completely customisable, and the results and graphs will be re drawn to accommodate the new values.

Not only does Numerical Methods Helper give a final result to an algorithm, but it also gives the calculations needed for students to get the final answer for themselves. For example, all integration algorithms are accompanied with list of ordinates used in the algorithm, along with a final formula that can be typed into any calculator to yield the final same final result.

Functions for use in any of the algorithms can be freely customised and changed using a number of built operators including trigonometry and logarithms. Not only can the program be used for the tuition of numerical methods, but the graphs can also be utilised for other topics such as transformations of graphs.

Main Features -

  • Freely scrollable and zoomable graphing view
  • Worked examples of all numerical algorithms and AS and A2 Maths including -
    • The Trapezium rule
    • The Mid - Ordinate Rule
    • Simpsons Rule
    • Root Finding by iteration
  • Teaching aids for other topics such as local extrema and tangents and normal lines to functions
  • Every parameter for each algorithm is fully customisable to meet a wide range of questions
  • Functions for use in any algorithm can be changed using a wide range of built in operators
  • Graphing views can be copied to the clipboard to paste into other applications for annotation and printing
  • Main parameters for algorithms can be modified by dragging points on the graph itself. This allows for a more interactive and visual approach to the progress and inaccuracies of the algorithm
  • Numerical data used in the calculations of the results of the algorithm is also provided to allow students themselves to reach a solution

Numerical Methods Helper Screenshot

This screenshot shows the integration tab of the program. The user is able to enter a custom function along with the upper and lower bounds of the integration, the numerical algorithm to use and the number of iterations. The results are then shown to the user, with a formula of the chosen algorithm and the ordinates used. Te application of the algorithm to the equation is also shown graphically on an interactive plotting view.

For more information about the application, download the User Manual along with the executable from GitHub or as a zip file.

Read More