• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Unit test
 

Unit test

on

  • 1,258 views

 

Statistics

Views

Total Views
1,258
Views on SlideShare
1,258
Embed Views
0

Actions

Likes
1
Downloads
16
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />

Unit test Unit test Presentation Transcript

  • 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