lundi 31 juillet 2017

How to mock function only in tested class and not in unittest case

So i have unit test case class and a class that I want to test. There is a function in this class that has a function that I'm using to run this method that i am testing. To visualize the problem i will write some abstract code below:

import needed_module
from casual_class import CasualClass

class CasualClassTests(unittest.TestCase):

    def setUp(self):
        self.casual_class = CasualClass()

    def _run(self, func):
        result = needed_module.runner(func)
        return result

    @mock.patch('needed_module.runner', return_value='test_run_output')
    def test_get_that_item(self, mocked_runner):
        result = self._run(self.casual_class.get_that_item())

And the tested class:

import needed_module


class CasualClass:

    def get_that_item(self):
        #..some code..
        run_output = needed_module.runner(something)
        return something2

In this code get_that_item code wont even run because the runner is mocked. What i want to achieve is to run needed_module.runner original in test case and mocked one in the tested class. I've searched the internet for too long, to solve this...

Aucun commentaire:

Enregistrer un commentaire