[IronPython] Unit testing IP hosting -- update

Fernando Correia fernandoacorreia at gmail.com
Mon Mar 17 13:50:56 CET 2008


I've sent some messages to this list about problems trying to use
NUnit to test IronPython hosting with IronPython-2.0A8.

Just so you know, with IronPython-2.0B1 that seems to be working, at
least with a very basic test.

Code follows.

Class1.cs
----------
using NUnit.Framework;

namespace UnitTest
{
    [TestFixture]
    public class Class1
    {
        [Test]
        public void TestHosting()
        {
            Host host = new Host();
            string x = host.Test();
            Assert.AreEqual("Hosting", x);
        }
    }
}


Host.cs
----------
using IronPython;
using IronPython.Modules;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;

namespace UnitTest
{
    public class Host
    {
        public string Test()
        {
            ScriptRuntime environment = ScriptRuntime.Create();
            ScriptEngine engine = environment.GetEngine("py");
            ScriptScope scope = environment.CreateScope();
            string scriptText = @"
def Test():
    return 'Hosting'
";
            ScriptSource script =
engine.CreateScriptSourceFromString(scriptText,
SourceCodeKind.Statements);
            CompiledCode compiledScript = script.Compile();
            compiledScript.Execute(scope);
            string functionCall = "Test()";
            ScriptSource function =
engine.CreateScriptSourceFromString(functionCall,
SourceCodeKind.Expression);
            return function.Execute(scope).ToString();
        }
    }
}



More information about the Ironpython-users mailing list