Home > database >  Python Unit testing script that calls some functions
Python Unit testing script that calls some functions

Time:01-11

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?

CodePudding user response:

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
  •  Tags:  
  • Related