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

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

  • I think the answer depends on what you want to test. What is the behavior of get_latest_file() that you want to test? Why are you considering mocks in the first place?
    – wovano
    Nov 12, 2022 at 11:18
  • @wovano, I was wondering if it is possible to test the logic without actually connecting to a real host. Say, getting some dummy file list and check whether 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? Nov 22, 2022 at 12:02
  • In that case, the main problem is that your code violates the Single-responsibility principle: your method connects to a host AND process the returned data. This could easily be solved by splitting your code into two methods: one that handles the connection and the other that process the data. In that case you could easily (unit)test that method without having to mock anything. (You might want to read more about Design For Testability as well).
    – wovano
    Nov 22, 2022 at 12:16
  • If you really do not want to change your code, you should mock the 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 :-)
    – wovano
    Nov 22, 2022 at 12:17
  • Thanks for your helpful comments and pointers. Yes, mocking with RobotFramework is a different challenge altogether. Nov 22, 2022 at 15:42


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.