Example IronPython Test Script

.NET Dynamic Language Runtime

RunTests.py

import sys
import unittest
import clr
sys.path.Add('C:\\sut') # folder containing software to be tested
clr.AddReferenceToFile('Polyglot.dll')

from Polyglot import Greet

class GreetTest(unittest.TestCase):
    softwareUnderTest = Greet
    def testGreet_ShowCsGreeting_GreetWithName(self):     
        self.assertEquals('Hello Brian', self.softwareUnderTest.GreetMe('Brian'), 'Greet Me test')        
    def testGreet_ShowCsGreeting_GreetHello(self):  
        self.assertEquals('Hello World!', self.softwareUnderTest.Hello, 'Greet Hello test')

from Polyglot import Math

class MathTest(unittest.TestCase):
    softwareUnderTest = Math()
    def testMath_Add_Simple(self):
        self.assertEquals(3, self.softwareUnderTest.Add(1,2), 'Simple Add')
    def testMath_Subtract_Simple(self):
        self.assertEqual(3, self.softwareUnderTest.Subtract(5,2), 'Simple Subtract')
    def testMath_Subtract_NegativeResultFails(self):
        with self.assertRaises(ValueError):
            self.softwareUnderTest.Subtract(3, 4)
    def testMath_Results_IsSetAndOrdered(self):
        result1 = 2
        result2 = 3
        result3 = 4
        self.softwareUnderTest = Math()
        self.assertEqual(0, len(self.softwareUnderTest.Results)) # to begin with there are no results
        self.softwareUnderTest.Add(1,1)
        self.assertEqual(1, len(self.softwareUnderTest.Results)) # after one operation there will be one result
        self.assertEqual(result1, self.softwareUnderTest.Results[0]) # check the result

        # run a couple more operations and check they are stored propery
        self.softwareUnderTest.Subtract(5,2)
        self.softwareUnderTest.Add(2,2)
        self.assertEqual(result3, self.softwareUnderTest.Results[0])
        self.assertEqual(result2, self.softwareUnderTest.Results[1])
        self.assertEqual(result1, self.softwareUnderTest.Results[2])

try:
    if __name__ == '__main__':
        unittest.main()
except:
    e = sys.exc_info()[0]
    print( "Error: %s" % e )
    clr.ClearProfilerData() # unload the .dll file so the file lock is released

a=raw_input('The end!')

.NET Common Language Runtime

These are the C# classes that have been specified by the preceding test. You will just need to create a C# Class Library project. In the project properties under Build Events, add the following code to the Post Build Event command line:

copy $(TargetPath) C:\sut
copy $(TargetDir)$(TargetName).pdb C:\sut

Greet.cs

namespace Polyglot
{
    public static class Greet
    {
        public static string GreetMe(string name)
        {
            return string.Format("Hello {0}", name);
        }

        public static string Hello { get { return "Hello World!"; } }
    }
}

Math.cs

using System.Collections.Generic;

namespace Polyglot
{
    public class Math
    {
        List _results;
        public Math()
        {
            _results = new List();
        }

        public uint Add(uint x, uint y)
        {
            return lastResult = x + y;
        }

        public uint Subtract(uint x, uint y)
        {
            if (y > x)
                throw new System.ArgumentException("Operation would result in a negative result");

            return lastResult = x - y;
        }

        private uint lastResult
        {
            set
            {
                _results.Insert(0, value);
            }
        }

        public uint[] Results
        {
            get { return _results.ToArray(); }
        }
    }
}
About these ads

One thought on “Example IronPython Test Script

  1. Pingback: Beginning IronPython | Real World Software

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s