【问题标题】:Instantiating a C# Object in Robot Framework在 Robot Framework 中实例化 C# 对象
【发布时间】:2016-12-22 06:20:47
【问题描述】:

我正在寻找使用 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。

【问题讨论】:

    标签: c# python .net robotframework python.net


    【解决方案1】:

    如果没有辅助实用程序,您可能无法在机器人中直接实例化 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多