5

I have a function in main module which takes in two values and performs opertaions on them.This uses a global variable which is created before calling this function

def calc(keys,values):
    if globalvar == "calc":
        return sum(keys)
    else:
        return sum(values)

Now in unittesting

class Testcalc(TestCase):
@mock.patch('module.globalvar ', "calc")
def test_unit(self,calc):
    keys=[1,2,3]
    values=[4,5,6]
    sum=module.calc(keys,values)
    """
    check asserts
    """

I am getting an type error with invalid arguments.

TypeError('test_unit() takes exactly 2 arguments (1 given)',)

Could anyone show me the correct way of mocking the global variable

Update: This worked for me not sure why

class Testcalc(TestCase):
@mock.patch('module.globalvar')
def test_unit(self,var):
    keys=[1,2,3]
    values=[4,5,6]
    var="calc"
    sum=module.calc(keys,values)
    """
    check asserts
    """

Thank you everyone

4
  • Show the full error. Commented Jul 27, 2018 at 15:43
  • TypeError('test_unit() takes exactly 2 arguments (1 given)',) Commented Jul 27, 2018 at 15:44
  • Please include the line that calls test_unit(), i.e. where you use it. Commented Jul 27, 2018 at 16:05
  • I don’t think your error is related to the way you’re mocking the global. But without a minimal reproducible example it’s hard to be sure. What you’ve posted here has an IndentationError, and a slightly variation on the same indentation problem could cause an error much like the one you’re seeing, but I doubt that’s your problem. So show us code that actually reproduces your problem instead of a different one. Commented Jul 27, 2018 at 16:13

1 Answer 1

9

A module.globalvar = 'anything' should be enough, no need to mock.patch

def test_calc2(self):
    keys = [1, 2, 3]
    values = [4, 5, 6]

    module.globalvar = "calc"
    sum = module.calc(keys, values)
    self.assertEqual(module.globalvar, 'calc')
    self.assertEqual(sum, 6)

    module.globalvar = 'other'
    sum = module.calc(keys, values)
    self.assertEqual(sum, 15)

This works using PropertyMock

@mock.patch('module.globalvar', new_callable=mock.PropertyMock)
def test_calc3(self, mocked_globalvar):

This is syntax right, but will fail the test, since the globalvar has to be set by PropertyMock

@mock.patch('module.globalvar')
def test_unit(self, mock_globalvar):
Sign up to request clarification or add additional context in comments.

2 Comments

Thank u for replying @Gang, So can I use module.globalvar = 'anything' in the test_unit function as a paramter and it would work instead of patch? def test_unit(self,var): keys=[1,2,3] values=[4,5,6] module.globalvar = 'calc' sum=module.calc(keys,values) """ check
@Ram, sorry the module.globalvar is right, I included in the update, the @mock.patch part is wrong, it only works with mock.PropertyMock for globalvars, class attributes etc

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.