Simon Tewsi Simon Tewsi - 2 months ago 18x
Python Question

Instantiating a C# Object in Robot Framework

I'm looking to use Robot Framework for testing .NET applications and I'm struggling to understand how Robot Framework can instantiate C# objects, to be used in testing.

The C# application I'm playing with is very simple:

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

I'm wanting to execute the AddStudent method from Robot Framework, passing in a Student object which should be saved to the database.

I've written a test library in Python that uses Python for .NET (pythonnet) to call the .NET application:

import clr
import sys

class SchoolGradesLibrary (object):

def __init__(self, application_path, connection_string):

self._application_path = application_path

# Need application directory on sys path before we can add references to the DLLs.

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):


Calling this from Python works:

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.
from Models import Student

student = Student()
student.StudentName = "Python Student"

I'm a bit lost as to how to do the same thing from Robot Framework. This is what I've got so far:

*** Variables ***
${APPLICATION_PATH} = C:\...\SchoolGrades\bin\Debug
${CONNECTION_STRING} = Data Source=...;Initial Catalog=...;Integrated Security=True

*** Settings ***

*** Test Cases ***
Add Student To Database
${student} = Student
${student.StudentName} = RF Student
Add Student ${student}

When I run this it fails with error message:
No keyword with name 'Student' found.

How can I create a Student object in Robot Framework, to pass to the Add Student keyword? Is there anything else obviously wrong with the test?

The C# application is written with .NET 4.5.1, the Python version is 3.5 and the Robot Framework version is 3.0.


You probably can't directly instantiate Student in robot without a helper utility, since Student is a class rather than a keyword.

The simplest solution is to create a keyword in SchoolGradesLibrary that creates the student:

import clr
from Models import Student

class SchoolGradesLibrary (object):
    def create_student(name=None):
        student = Student()
        if name is not None:
            student.StudentName = name
        return student

You could then use that in your test case like a normal keyword.

${student}=    create student    Inigo Montoya