UNIT TEST
davidx <mykingheaven@gmail.com>
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()
:“
     ”
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, 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()
#!/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()
Test Suite
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))
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/index.html

Python Unit Test

  • 1.
  • 2.
    import random import unittest classTestSequenceFunctions(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.
    :“
  • 6.
    Test Fixture Test Case TestSuite Test Runner
  • 7.
  • 9.
  • 10.
    , ,
  • 11.
    , , , ,
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
    python module unittest
  • 20.
    (unittest.TestCase) setUp, tearDown suite ( , )
  • 21.
  • 22.
  • 25.
    #!/usr/bin/python #-*-coding:utf-8-*- import httplib import simplejsonas 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()
  • 27.
    #!/usr/bin/python #-*-coding:utf-8-*- import unittest from linkapnimport 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()
  • 32.
  • 33.
    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))
  • 35.
  • 36.
  • 37.
  • 38.