0


I have gone through docs but did not understand unittesting in real sense in python.
i have a test code can anyone will tell me how to do unittestng on it?

a = 1  
b = 2  
def test():  
  c = a + 2  
  if c > 5:  
    z = 7   
  else:  
    z = 8  
  answer = b + z  
  return answer 
3
  • 2
    Your test code always does the same thing, so the unit test would be very simple: check that test() returns what it's supposed to return. A more practical example would be if test() took two arguments (test(a, b)). In that case you'd try it with several different arguments and check that it returns the expected value in all cases. Commented May 14, 2011 at 21:54
  • i didnt understand you.if lets say I pass a variable d to test and use that d also in answer = b+z+d. i want to chk if that d should not be invalid like string or float. how can i create test. kindly give me example of it. Commented May 15, 2011 at 6:39
  • From your comments on this question, I get the feeling that you should first study what unit testing is, then study Python in some more depth and then revisit this problem you're having. Commented May 15, 2011 at 10:36

2 Answers 2

1

To test your test() method you should create a test file like this

import unittest
from your_file import test

class TestMethodTestCase(unittest.TestCase):
    def test_01a(self):
       """ test the test method"""
       self.failUnlessEqual(9, test(a=4, b=2))   # here you write all the use case you need to be sure that your method is correctly doing the job
       self.failUnlessEqual(10, test(a=1, b=2))
       self.failUnlessEqual(11, test(a=5, b=3))

if __name__=="__main__":
    unittest.main()

with your test() method defined like this :

def test(a=1, b=2):  
  c = a + 2
  if c > 5:
    z = 7
  else:  
    z = 8  
  answer = b + z  
  return answer 

Have a look to python unittest documentation

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

4 Comments

hey wats this 10, 9, 8 u passed? we only pass functional parameter then waht r those?
the first parameter of failUnlessEqual() is the expected result of the tested method.
oh ok. and if i wanted expected result to be not less than zero and not more than 10 then?
You can use the assertGreater methods if you are in python > 2.7 : docs.python.org/library/…
0

Unit testing doesn't mean much more than just automated checking of the output of various functions in your code when they are given certain values. Basically, you just want to make sure that those tests don't break when you're changing things.

2 Comments

means if i use name__=='__main':, will it called as unit testing?
No. The __main__ trick is used to prevent modules from executing if you import them.

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.