0

I have used the following suggestion to set up my code: Checking whether function has been called multiple times with different parameters

I would like to test how many times the function write_standalone_end_of_event is called.

#exchanges.impl.bats.mcformat.py
def delete_data(...): 
    exg = exchanges.impl.bate.parser.Exchange
    securities = [one_item]
    
    for security in securities: 
        exg.write_standalone_end_of_event


#unit_test_package
with patch('exchanges.impl.bate.parser.Exchange.write_standalone_end_of_event') as function:
    bats.protocol.sec_id_in_batch = {1: 'dummy1'}
    bats.protocol.transaction_end(exg, fields, values, msgtype, sequence)
    assert function.gacs.call_count == 1

When I run the above, i get an asserTion error that 0 != 1.

I can't seem to figure out why the function is not picking up the one call to it, even though when I debug it, it does seem to call write_standalone_end_of_event one time.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Patrick_Chong
  • 434
  • 2
  • 12
  • You haven't provided enough code here for us to reproduce the problem. You're asking for `function.gacs.call_count`, but what is the `gacs` method? I don't see that called anywhere in your code. – larsks Sep 07 '22 at 11:41
  • @larsks, actually I'm not sure what it is either- could you kindly have a brief look at the link above? - I tried to replicate their code and they had `gacs` in it, but I wasn't sure what it represented. – Patrick_Chong Sep 07 '22 at 11:44
  • Never mind- everything works now! - I realised it is part of the path. Once I took out `gacs`, the test passed. – Patrick_Chong Sep 07 '22 at 11:45
  • [SO Trace Function Calls in Python answer](https://stackoverflow.com/a/67064468/11815313) may help - e.g. `python3 -m trace --listfuncs example.py` - you'll then have to extract the call count for the function of interest, but that should be doable. – MagnusO_O Sep 07 '22 at 11:47
  • [SO How do I count the number of calls of a function in Python 3.6? answer from Nils Werner](https://stackoverflow.com/a/50856736/11815313) shows a solution using `invocations` and `functools` within the py script. – MagnusO_O Sep 07 '22 at 12:38

0 Answers0