简体   繁体   中英

Python Unit testing script that calls some functions

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM