2

In the following code,

1.If we assert the end result of a function then is it right to say that we have covered all lines of the code while testing or we have to test each line of the code explicitly if so how ?

2.Also Is it fine that we can have the positive ,negative and more assert statements in one single test function.If not please give examples

def get_wcp(book_id):
    update_tracking_details(book_id)
    c_id = get_new_supreme(book_id)
    if book_id == c_id:
       return True
    return False

class BookingentitntyTest(TestCase):
    def setUp(self):
       self.booking_id = 123456789# 9 digit number poistive test case

    def test_get_wcp(self):
       retVal = get_wcp(self.book_id)
       self.assertTrue( retVal )
       self.booking_id = 1# 1 digit number Negative test case
       retVal = get_wcp(self.book_id)
       self.assertFalse( retVal )
2
  • For your method , on a coverage point of view , if you test that method for both book_id == c_id and book_id != c_id , then you can assure that you are covering all the code paths. Commented Sep 29, 2017 at 10:26
  • unrelated, but if book_id == c_id: return True; return False can be simplified to return book_id == c_id Commented Sep 29, 2017 at 11:08

1 Answer 1

2
  1. No, just because you asserted the final result of the statement doesn't mean all paths of your code has been evaluated. You don't need to write "test each line" in the sense that you only need to go through all the code paths possible.

  2. As a general guideline, try to keep the number of assert statements in a test as minimum as possible. When one assert statement fails, the further statements are not executed, and doesn't count as failures which is often not required.

Besides try to write your tests as elegantly as possible, even more so than the code they are testing. We wouldn't want to write tests to test our tests, now would we?

  def test_get_wcp_returns_true_when_valid_booking_id(self):
      self.assertTrue(get_wcp(self.book_id))

  def test_get_wcp_returns_false_if_invalid_booking_id(self):
      self.assertFalse(get_wcp(1))

Complete bug-free code is not possible.

"Testing shows the presence, not the absence of bugs" - Edsger Dijkstra.

Sign up to request clarification or add additional context in comments.

3 Comments

So with your example of self.assertTrue(get_wcp(self.book_id)) are we saying that the entire function coverage is 100 % ? since we are not testing other individual methods l explicitly ike update_tracking_details(book_id) and get_new_supreme(book_id)
@Neo it depends. A function can contain innumerable number of extreme cases in which it can behave differently. It could be possible that the function breaks for a specific value of book_id say 0. So all we can do is try to guess as many test scenarios as possible. As Edgar Dijkstra said - "Testing proves the presence of bugs, not absence".
Nice quote "Testing proves the presence of bugs, not absence" by Edgar Dijkstra

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.