0

So I have this function which returns sine and cosine and another basic unittest function which is supposed to test both return values with assertEqual, but I don't know how to test both of the return values. What's the best way to do that?

def calc_sin_cos(sindeg, cosdeg):
    sine = math.sin(math.radians(sindeg))
    cosine = math.cos(math.radians(cosdeg))

    return sine, cosine

def test_calc_sin_cos(self):
    sine = 2
    cosine = 2
    result = myscript.calc_sin_cos(sine, cosine)
    self.assertEqual(result, 0.03489949670250097, 0.9993908270190958)

Obviously the above assertEqual doesn't work properly as it is.

1
  • It is important to understand, you can ever only have a single return value. You are returning a tuple of length 2 Commented Mar 10, 2021 at 17:40

1 Answer 1

2

When the method returns multiple values, it is a tuple in fact, the 2 following lines are identical:

return sine, cosine
return (sine, cosine)

For the calling method, these are same

result = myscript.calc_sin_cos(sine, cosine)
sin, cos = myscript.calc_sin_cos(sine, cosine)

To test them use assertEqual

  • with both values so as a tuple

    result = myscript.calc_sin_cos(sine, cosine)
    self.assertEqual(result, (0.03489949670250097, 0.9993908270190958))
    
  • or both values individually

    sin, cos = myscript.calc_sin_cos(sine, cosine)
    self.assertEqual(sin, 0.03489949670250097)
    self.assertEqual(cos, 0.9993908270190958)
    

For float values, it can use useful to use unittest.assertAlmostEqual

  self.assertAlmostEqual(sin, 0.034, places=3)
  self.assertAlmostEqual(cos, 0.999, places=3)
Sign up to request clarification or add additional context in comments.

Comments

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.