实例化机器人框架中的C#对象
问题描述:
我正在寻找使用Robot Framework来测试.NET应用程序,并且我正在努力了解Robot Framework如何实例化C#对象,以用于测试。实例化机器人框架中的C#对象
我与演出的C#应用程序非常简单:
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
我想从机器人框架执行传递addStudent方法,传递应保存到数据库中的Student对象。
我用Python编写的测试库,它使用Python for .NET (pythonnet)调用.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 Framework做同样的事情。这是我到目前为止有:
*** 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}
当我运行这一点,失败,错误消息:No keyword with name 'Student' found.
我如何可以创建Robot Framework的一个Student对象,传递给学生添加关键字?测试中还有其他明显的错误吗?
C#应用程序使用.NET 4.5.1编写,Python版本为3.5,Robot Framework版本为3.0。
答
由于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
工作就像一个魅力,谢谢。 –