在Robot框架中实例化C#对象
我希望使用Robot框架来测试.NET应用程序,我很难理解Robot框架如何实例化C#对象,以便在测试中使用 我正在使用的C#应用程序非常简单:在Robot框架中实例化C#对象,c#,python,.net,robotframework,python.net,C#,Python,.net,Robotframework,Python.net,我希望使用Robot框架来测试.NET应用程序,我很难理解Robot框架如何实例化C#对象,以便在测试中使用 我正在使用的C#应用程序非常简单: SystemUnderTest solution |_ DataAccess project (uses Entity Framework to connect to database) | |_ SchoolContext class | |_ Models project |_ Student class | |_ SchoolGrades
SystemUnderTest solution
|_ DataAccess project (uses Entity Framework to connect to database)
| |_ SchoolContext class
|
|_ Models project
|_ Student class
|
|_ SchoolGrades project (class library)
|_ SchoolRoll class
|_ AddStudent(Student) method
我想从Robot框架中执行AddStudent方法,传入一个Student对象,该对象应该保存到数据库中
我用Python编写了一个测试库,用于调用.NET应用程序:
import clr
import sys
class SchoolGradesLibrary (object):
def __init__(self, application_path, connection_string):
self._application_path = application_path
sys.path.append(application_path)
# Need application directory on sys path before we can add references to the DLLs.
clr.AddReference("SchoolGrades")
clr.AddReference("DataAccess")
clr.AddReference("Models")
from SchoolGrades import SchoolRoll
from DataAccess import SchoolContext
from Models import Student
context = SchoolContext(connection_string)
self._schoolRoll = SchoolRoll(context)
def add_student(self, student):
self._schoolRoll.AddStudent(student)
从Python调用此函数可以:
from SchoolGradesLibrary import SchoolGradesLibrary
import clr
application_path = r"C:\...\SchoolGrades\bin\Debug"
connection_string = r"Data Source=...;Initial Catalog=...;Integrated Security=True"
schoolLib = SchoolGradesLibrary(application_path, connection_string)
# Have to wait to add reference until after initializing SchoolGradesLibrary,
# as that adds the application directory to sys path.
clr.AddReference("Models")
from Models import Student
student = Student()
student.StudentName = "Python Student"
schoolLib.add_student(student)
对于如何从Robot框架做同样的事情,我有点不知所措。到目前为止,我得到的是:
*** Variables ***
${APPLICATION_PATH} = C:\...\SchoolGrades\bin\Debug
${CONNECTION_STRING} = Data Source=...;Initial Catalog=...;Integrated Security=True
*** Settings ***
Library SchoolGradesLibrary ${APPLICATION_PATH} ${CONNECTION_STRING}
*** Test Cases ***
Add Student To Database
${student} = Student
${student.StudentName} = RF Student
Add Student ${student}
当我运行此操作时失败,并显示错误消息:未找到名为“Student”的关键字。
如何在Robot框架中创建一个学生对象,以传递给AddStudent关键字?考试还有什么明显的问题吗
C#应用程序是用.NET 4.5.1编写的,Python版本是3.5,Robot框架版本是3.0。如果没有助手实用程序,您可能无法在Robot中直接实例化
Student
,因为Student
是一个类而不是一个关键字
最简单的解决方案是在SchoolGradesLibrary中创建一个关键字来创建学生:
...
import clr
clr.AddReference("Models")
from Models import Student
...
class SchoolGradesLibrary (object):
...
def create_student(name=None):
student = Student()
if name is not None:
student.StudentName = name
return student
...
然后,您可以在测试用例中像普通关键字一样使用它
${student}= create student Inigo Montoya