Problem with running Selenium python unittests in C # using ScriptEngine (.NET 3.5)

first time poster.

I turn to the first stack overflow question because I found few resources trying to find the answer. I want to run Selenium python tests from a C # application. I do not want to collect C # Selenium tests every time; I want to use IronPython scripts to dynamically test selenium. (note: I have little Python or ScriptEngine, etc.).

Selenium prints unit tests in python as follows:

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 run this, without problems, from the command line using ipy.exe:

 ipy test_google.py 

And I see that Selenium Server launches the Firefox browser instance and runs the test.

I cannot achieve the same result using ScriptEngine and the API in C #, .NET 3.5, and I think that it is centered around the inability to execute the main () function, I assume the following code:

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

I tried engine.ExecuteFile (), engine.CreateScriptSourceFromString () / source.Execute () and engine.CreateScriptSourceFromFile () / source.Execute (). I tried scope.SetVariable (" __name__ ", " __main__ "). I get some success when commenting on if if the __name__ part of the py file calls engine.CreateScriptSourceFromString ("unittest.main (module = None)) after engine.Runtime.ExecuteFile () is called in the py file. I tried to save the results in python and access them through scope.GetVariable (). I also tried to write a python function that I could call from C # to run unit tests.

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

My ignorance of Python, ScriptEngine or the unittest module can easily be behind my problems. Has anyone been lucky to run python unittests using ScriptEngine etc. API in C #? Has anyone successfully executed the "main" code from ScriptEngine?

In addition, I read that unittest has a test runner that will help in accessing errors through the TestResult object. I believe the syntax is as follows. I have not reached yet, but I know that I need to collect the results.

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

Thanks in advance. I thought it was better to have more details than less. = P

+4
source share
2 answers

Looking at the source code on the IronPython console (ipy.exe), it looks like it ultimately comes down to calling ScriptSource.ExecuteProgram() , you can get the ScriptSource from any of the various ScriptEngine.CreateScriptSourceFrom* methods.

For instance:

 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 ".

+2
source

Try the following:

 unittest.main(module=__name__) 
0
source

All Articles