1

I ended up with testing an "automation" script in python, basically just a script calling bunch of functions.

I have 3 classes: class_A, class_B, class_C, with each class having a "run" function

Script.py is calling class_A.run(), class_B.run, class_C.run()

My question would be if is there is a way of unit testing Script.py to just assert if the 3 run functions we're called, without actually running (going trough their code) them.

I tried patching the class and i can get the correct assert, but the run functions are still "running" their code.

Is it possible to somehow Mock the class_A.run() and assert if was called?

1
  • Please provide a mvce Commented Jan 7, 2022 at 2:51

1 Answer 1

1

You could use Mock patch. The MockClass will replace your module.class_A and the run-method:

from unittest.mock import patch

@patch('module.class_A', 'run')
def test(MockClass):
    Script.py  # your testfunction 
    assert MockClass.run.called  # check if module.Class_A.run() was called
Sign up to request clarification or add additional context in comments.

1 Comment

Mention: path for class A in my case was "python_framework.utils.Class_A" and path for "Script.py" is "python_framework.Script". Once i patched the function using "pyton_framework.Script.Class_A.run" everything worked. *Before i was patching "python_framework.utils.Class_A.run"

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.