You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file explores using Python's unit testing framework, PyTest, in conjunction with OpenTAP's Python resources.
OpenTAP was built using the .NET framework; Python capabilities were later added via the Python.NET library.
Python.NET is a bridge between Python and .NET platforms, enabling interoperation between the two. This tool allows Python programs to access the .NET Framework and Mono libraries; similarly, it provides .NET applications the ability to utilize Python libraries.
Details
This demonstration is using Windows 11
This demonstration is using Python 3.11.3
Simple Python.NET Example
The following example imports the Common Language Runtime (CLR) module from .NET via Python.NET. Afterward, it constructs a string using the .NET framework then uses that object's built-in length method.
string_dotnet.py
'''NAME: string_dotnet.pyOVERVIEW: This script explores the use of the Python.NET library in Python.DETAILS: Python.NET is a bridge between Python and .NET platforms, enabling interoperation between the two. This tool allows Python programs to access the .NET Framework and Mono libraries; similarly, it provides .NET applications the ability to utilize Python libraries. This script imports the Common Language Runtime (CLR) module from .NET. The function `string_length(s)` illustrates this interoperation. It constructs a string `s` via CLR from .NET, and then returns the length of this string.'''importclrclr.AddReference('System')
fromSystemimportStringdefstring_length(s):
dotnet_string=String(s)
returndotnet_string.Length
main.py
'''NAME: main.pyOVERVIEW: This program is the driver to test `string_dotnet.py`.DETAILS: Expected output is 12. See `string_dotnet.py` for details.'''importclrimportstring_dotnetasdotnetclr.AddReference("System")
fromSystemimportConsoleprint(dotnet.string_length("Hello World!"));
Output
python main.py12
Simple PyTest with Python.NET Resources
Continue using main.py and dotnet_string.py from the Simple Python.NET Example section.
The following content uses Python's built-in unit testing framework, PyTest, to test our aformentioned files.
Section successfully demonstrates that we can use PyTest in conjunction with Python.NET.
# TestStepExample.pyfromopentapimport*fromSystemimportInt32importOpenTapfrom .TestInstrumentimportTestInstrument@attribute(OpenTap.Display("Test Step Example", "A simple python test step", "demo"))classTestStepExample (TestStep):
my_test_instrument=property(TestInstrument).\
add_attribute(OpenTap.Display(
"Instrument", "The instrument to use in the step.", "Resources"))
def__init__(self):
super(TestStepExample, self).__init__()
defRun(self):
self.log.Debug("Number of characters: {0}", Int32(self.my_test_instrument.string_length("Hello World!")))
TestInstrument.py
# TestInstrumnet.pyfromopentapimport*fromSystemimportStringimportOpenTapimportclrclr.AddReference("demo.Api")
fromdemo.ApiimportPythonInstrumentApi@attribute(OpenTap.Display("Test Instrument", "This class implements a shared Python API.", "demo"))classTestInstrument(Instrument, PythonInstrumentApi):
def__init__(self):
"Set up the properties, methods and default values of the instrument."super(TestInstrument, self).__init__()
self.Name="Test Instrument"defstring_length(self, s):
dotnet_string=String(s)
returndotnet_string.Length
Module opentap lives in the default OpenTAP isntallation directory at C:\Program Files\OpenTAP\Packages\Python
Module opentap is a .NET bridge iteroperating the OpenTAP .NET library so that it can be used by Python.
I attempted to add the resource into my Python interpert's path but it failed. The module has a dynamically linked library dependencies; I think this is C:\Program Files\OpenTAP\Packages\Python\OpenTap.Python.dll. Adding this file is likely not a practical solution... It could be though if organized properly but may be too large of an undertaking.