views:

208

answers:

1

Hello all, first time poster.

I'm turning to my first question on stack overflow because I've found little resources in trying to find an answer. I'm looking to execute Selenium python tests from a C# application. I don't want to have to compile the C# Selenium tests each time; I want to take advantage of IronPython scripting for dynamic selenium testing. (note: I have little Python or ScriptEngine, et al experience.)

Selenium outputs unit tests in python in the following form:

from selenium import selenium
import unittest

class TestBlah(unittest.TestCase):
    def setUp(self):
        self.selenium = selenium(...)
        self.selenium.start()

    def test_blah(self):
        sel = self.selenium
        sel.open("http://www.google.com/webhp")
        sel.type("q", "hello world")
        sel.click("btnG")
        sel.wait_for_page_to_load(5000)
        self.assertEqual("hello world - Google Search", sel.get_title())
        print "done"

    def tearDown(self):
        self.selenium.stop()

if __name__ == "__main__":
    unittest.main()

I can get this to run, no problem, from the command line using ipy.exe:

ipy test_google.py

And I can see Selenium Server fire up a firefox browser instance and run the test.

I cannot achieve the same result using the ScriptEngine, et al API in C#, .NET 3.5, and I think it's centered around not being able to execute the main() function I'm guessing the following code is:

if __name__ == "__main__":
    unittest.main()

I've tried engine.ExecuteFile(), engine.CreateScriptSourceFromString()/source.Execute(), and engine.CreateScriptSourceFromFile()/source.Execute(). I tried scope.SetVariable("__name__", "__main__"). I do get some success when I comment out the if __name__ part of the py file and call engine.CreateScriptSourceFromString("unittest.main(module=None") after engine.Runtime.ExecuteFile() is called on the py file. I've tried storing the results in python and accessing them via scope.GetVariable(). I've also tried writing a python function I could call from C# to execute the unit tests.

(engine is an instance of ScriptEngine, source an instance of ScriptSource, etc.)

My ignorance of Python, ScriptEngine, or the unittest module could easily be behind my troubles. Has anyone had any luck executing python unittests using the ScriptEngine, etc API in C#? Has anyone successfully executed "main" code from ScriptEngine?

Additionally, I've read that unittest has a test runner that will help in accessing the errors via a TestResult object. I believe the syntax is the following. I haven't gotten here yet, but know I'll need to harvest the results.

unittest.TextTestRunner(verbosity=2).run(unittest.main())

Thanks in advance. I figured it'd be better to have more details than less. =P

A: 

Looking at the source code to the IronPython Console (ipy.exe), it looks like it eventually boils down to calling ScriptSource.ExecuteProgram(). You can get a ScriptSource from any of the various ScriptEngine.CreateScriptSourceFrom* methods.

For example:

import clr
clr.AddReference("IronPython")

from IronPython.Hosting import Python
engine = Python.CreateEngine()
src = engine.CreateScriptSourceFromString("""
if __name__ == "__main__":
    print "this is __main__"
""")

src.ExecuteProgram()

Running this will print "this is main".

Jeff Hardy