Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unit test a function that does not return anything?

Is it possible to find the values of the local variables in a function by mocking?

class A:
  def f(self):
    a = 5000 * 10
    B().someFunction(a)

How do I write a unit test for this function? I have mocked someFunction as I do not want the testing scope to go outside the block. The only way I can test the rest of the function is by checking if the value of variable a is 50000 at the end of the function. How do I do this?

like image 659
Pradeep Vairamani Avatar asked Mar 24 '14 10:03

Pradeep Vairamani


2 Answers

A function that does not return anything, doesn't modify anything and does not raise any error is a function that basically have no reason to be.

  • If your function is supposed to assert something and raise an error, give it wrong information and check if it does raise the right error.
  • If your function takes an object and modifies it, test if the new state of your object is as expected.
  • If your function output something to the console, you can temporarily redirect the input/output stream and test what is written/read.

If none of the above, just delete your function and forget about it :)

like image 71
Samy Arous Avatar answered Oct 07 '22 02:10

Samy Arous


With interaction testing, you could check what value someFunction was called with. What happens inside that function, should be tested in the unit test of that function.

like image 26
elias Avatar answered Oct 07 '22 02:10

elias