在C#中使用的ScriptEngine(.NET 3.5)执行麻烦硒蟒蛇单元测试 [英] trouble executing Selenium python unittests in C# with ScriptEngine (.NET 3.5)
问题描述
第一次海报。
我转向我的堆栈溢出的第一个问题,因为我发现很少的资源,试图找到答案。我期待从C#应用程序执行硒蟒蛇测试。我不想每次都编译C#Selenium测试;我想利用IronPython的脚本进行动态测试硒。 (注:我很少有Python或的ScriptEngine等经验。)
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.)
硒输出单元测试在Python中的格式如下:
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()
我可以使用ipy.exe得到这个跑,没问题,在命令行:
I can get this to run, no problem, from the command line using ipy.exe:
ipy test_google.py
和我能看到硒服务器启动一个Firefox浏览器实例并运行测试。
And I can see Selenium Server fire up a firefox browser instance and run the test.
我不能达到使用的ScriptEngine等在C#API,.NET 3.5相同的结果,我想它周围不能够执行我猜下面的代码在main()函数中心是:
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()
我试过engine.ExecuteFile( ),engine.CreateScriptSourceFromString()/ source.Execute(),和engine.CreateScriptSourceFromFile()/ source.Execute()。我试图scope.SetVariable( __ __名
, __ __主要
)。我做的时候我注释掉engine.Runtime如果 __名
的PY文件的一部分,并呼吁engine.CreateScriptSourceFromString(unittest.main(模块=无)后__获得一些成功.ExecuteFile()被调用的PY文件。我已经试过存储蟒蛇的结果,并通过scope.GetVariable访问它们()。我也试着写一个Python的功能,我可以从C#调用执行单元测试。
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.
(发动机的ScriptEngine的实例,源ScriptSource的一个实例,等。)
(engine is an instance of ScriptEngine, source an instance of ScriptSource, etc.)
我的无知Python中,的ScriptEngine,或unittest模块可以很容易地背着我的烦恼。有没有人有任何运气执行使用的ScriptEngine等在C#中API的Python单元测试?有没有人成功地执行从的ScriptEngine主的代码?
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?
此外,我读过,单元测试有一个测试运行,将在通过的TestResult对象访问错误的帮助,我相信语法如下,我这里没有得到还没有,但知道我需要收获的结果。
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())
提前
感谢。我想这会是最好有更多的细节以内。 = P
Thanks in advance. I figured it'd be better to have more details than less. =P
推荐答案
综观的源代码到IronPython的控制台(ipy.exe),它看起来像它最终归结为调用 ScriptSource.ExecuteProgram()
。你可以得到一个 ScriptSource
从任意的各种 ScriptEngine.CreateScriptSourceFrom *
方法。
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.
例如:
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()
运行这个将打印这是主要
这篇关于在C#中使用的ScriptEngine(.NET 3.5)执行麻烦硒蟒蛇单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!