在 Robot Framework 中实例化 C# 对象
Instantiating a C# Object in Robot Framework
我希望使用 Robot Framework 来测试 .NET 应用程序,并且我正在努力了解 Robot Framework 如何实例化 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
我想从 Robot Framework 执行 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 对象,以传递给 Add Student 关键字?测试还有其他明显的问题吗?
C#应用程序使用.NET 4.5.1编写,Python版本为3.5,Robot Framework版本为3.0。
您可能无法在没有辅助实用程序的情况下直接在机器人中实例化 Student
,因为 Student
是 class 而不是关键字。
最简单的解决方案是在创建学生的 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
我希望使用 Robot Framework 来测试 .NET 应用程序,并且我正在努力了解 Robot Framework 如何实例化 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
我想从 Robot Framework 执行 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 对象,以传递给 Add Student 关键字?测试还有其他明显的问题吗?
C#应用程序使用.NET 4.5.1编写,Python版本为3.5,Robot Framework版本为3.0。
您可能无法在没有辅助实用程序的情况下直接在机器人中实例化 Student
,因为 Student
是 class 而不是关键字。
最简单的解决方案是在创建学生的 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