Your SlideShare is downloading. ×
Unit test
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Unit test

1,015
views

Published on

Published in: Technology

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,015
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
18
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide






































  • Transcript

    • 1. UNIT TEST davidx <mykingheaven@gmail.com>
    • 2. import random import unittest class TestSequenceFunctions(unittest.TestCase): def setUp(self): self.seq = range(10) def test_shuffle(self): # make sure the shuffled sequence does not lose any elements random.shuffle(self.seq) self.seq.sort() self.assertEqual(self.seq, range(10)) # should raise an exception for an immutable sequence self.assertRaises(TypeError, random.shuffle, (1,2,3)) def test_choice(self): element = random.choice(self.seq) self.assertTrue(element in self.seq) def test_sample(self): with self.assertRaises(ValueError): random.sample(self.seq, 20) for element in random.sample(self.seq, 5): self.assertTrue(element in self.seq) if __name__ == '__main__': unittest.main()
    • 3. :“ ”
    • 4. Test Fixture Test Case Test Suite Test Runner
    • 5. Test Fixture
    • 6. Test Case
    • 7. , , ,
    • 8. ,
    • 9. ,
    • 10. Test Suite
    • 11. Test Case
    • 12. Test Runner
    • 13. GUI
    • 14. Python
    • 15. python module unittest
    • 16. (unittest.TestCase) setUp, tearDown suite ( , )
    • 17. setUp , ,
    • 18. tearDown
    • 19. #!/usr/bin/python #-*-coding:utf-8-*- import httplib import simplejson as json from socket import socket import struct, ssl, binascii class APN(object): _conn = None def __init__(self, server='gateway.sandbox.push.apple.com', port=2195): self._server = server self._port = port self._conn = self._get_connection() def _get_connection(self): if not self._conn: s = socket() c = ssl.wrap_socket(s, ssl_version = ssl.PROTOCOL_SSLv3, certfile ='apn_mallsales.pem') c.connect((self._server, self._port)) self._conn = c return self._conn def send_message(self, udid, message, badge=9): body = { 'aps': { 'alert': message, 'badge': badge } } payload = json.dumps(body) fmt = "!cH32sH%ds" % len(payload) command = 'x00' msg = struct.pack(fmt, command, 32, binascii.unhexlify(udid.replace(' ','')), len(payload), payload) self._get_connection().write(msg) def close(self): if self.__conn: self.__conn.close()
    • 20. #!/usr/bin/python #-*-coding:utf-8-*- import unittest from linkapn import APN class TestApn(unittest.TestCase): def setUp(self): self.apn = APN() self.udid = '4d5479aa 5509cc3f 43ea363d 55406e98 13252481 063c3089 9c317478 736fa722' def test_one(self): message = 'test from david' badge = 9 self.apn.send_message(self.udid, message, badge) def test_two(self): message = 'another test from david' badge = 9 self.apn.send_message(self.udid, message, badge) def tearDown(self): print "closing connection" if __name__ == '__main__': unittest.main()
    • 21. Test Suite
    • 22. def suite(): suite = unittest.TestSuite() suite.addTest(TestApn('first_test')) suite.addTest(TestApn('second_test')) return suite or: def suite(): tests = ['first_test', 'second_test'] return unittest.TestSuite(map(TestApn, tests))
    • 23. tdd -
    • 24. Agile -
    • 25. Questions?
    • 26. http://www.hudong.com/wiki/%E5%8D %95%E5%85%83%E6%B5%8B%E8%AF %95 http://www.ibm.com/developerworks/cn/ linux/l-pyunit/index.html