UNIT TEST
davidx <mykingheaven@gmail.com>
import random
import unittest

class TestSequenceFunctions(unittest.TestCase):
    def setUp(self):
        self.seq = ran...
:“
     ”
Test Fixture

Test Case

Test Suite

Test Runner
Test Fixture
Test Case
,       ,




    ,
,
,
Test Suite
Test Case
Test Runner
GUI
Python
python     module

unittest
(unittest.TestCase)

                setUp, tearDown



    suite        (
,           )
setUp


,   ,
tearDown
#!/usr/bin/python
#-*-coding:utf-8-*-
import httplib
import simplejson as json
from socket import socket
import struct, ss...
#!/usr/bin/python
#-*-coding:utf-8-*-
import unittest
from linkapn import APN
class TestApn(unittest.TestCase):
   def set...
Test Suite
def suite():
	

 suite = unittest.TestSuite()
	

 suite.addTest(TestApn('first_test'))
	

 suite.addTest(TestApn('second_te...
tdd -
Agile -
Questions?
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/in...
Unit test
Unit test
Unit test
Unit test
Unit test
Unit test
Unit test
Unit test
Unit test
Unit test
Unit test
Upcoming SlideShare
Loading in …5
×

Unit test

1,084
-1

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

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

No notes for slide






































  • Unit test

    1. 1. UNIT TEST davidx <mykingheaven@gmail.com>
    2. 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. 3. :“ ”
    4. 4. Test Fixture Test Case Test Suite Test Runner
    5. 5. Test Fixture
    6. 6. Test Case
    7. 7. , , ,
    8. 8. ,
    9. 9. ,
    10. 10. Test Suite
    11. 11. Test Case
    12. 12. Test Runner
    13. 13. GUI
    14. 14. Python
    15. 15. python module unittest
    16. 16. (unittest.TestCase) setUp, tearDown suite ( , )
    17. 17. setUp , ,
    18. 18. tearDown
    19. 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. 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. 21. Test Suite
    22. 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. 23. tdd -
    24. 24. Agile -
    25. 25. Questions?
    26. 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
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×