I have created a RobotFramework test library in Python. It internally utilizes paramiko and paramiko-expect packages to connect to various *nix hosts and execute Unix commands.
I have been reading about Python's unittest.mock library but having issues in how to write sample mock tests to test my custom keyword functions using it. The idea is to run the tests quickly so that I don't have to wait for a long time for the tests to run between code changes.
For e.g., I have a base Command library in Command.py as follows,
from typing import List, Optional
class Command:
ROBOT_LIBRARY_VERSION = '1.0'
ROBOT_LIBRARY_SCOPE = 'GLOBAL'
@staticmethod
def run_command(
command: str, timeout: int = 60, prompt: str = '.*[\$#>]\s+',
present_patterns: Optional[List[str]] = None
):
# Run the given command on host derived from a config file
# Fail the keyword if any of the present patterns are not present in the command output
@staticmethod
def get_last_command_output() -> str:
# return the last command output
I have specialized file operations library in FileOps.py,
from com.stackoverflow.Command import Command
class FileOps:
ROBOT_LIBRARY_VERSION = '1.0'
ROBOT_LIBRARY_SCOPE = 'GLOBAL'
@staticmethod
def get_latest_file(absolute_file_pattern: str) -> str:
Command.run_command(command=f'ls -lrt {absolute_file_pattern}')
output = Command.get_last_command_output()
# now split output lines and process the output to get the latest file
# on the last line and return
Now if I want to test the get_latest_file() through mock objects, what will be the approach as it is internally using run_command and other calls too?
I am not sure whether I can do mock testing through the robot files.
get_latest_file()
that you want to test? Why are you considering mocks in the first place?get_latest_file()
is able to extract the last filename in the list properly. Will it be a good case for mock? If yes, how?run_command()
method. Unfortunately, I'm not very familiar with the RobotFramework, so I can't provide a full answer for that. For me personally, changing the code would be a lot easier :-)