Skip to content

Instantly share code, notes, and snippets.

@zhenghao1
Last active December 26, 2015 23:19
Show Gist options
  • Save zhenghao1/7229735 to your computer and use it in GitHub Desktop.
Save zhenghao1/7229735 to your computer and use it in GitHub Desktop.
Sample python unittest written using nose framework
#-*- coding: utf-8 -*-
import unittest
import redis
from apps.mylib.client import Redis
from nose.tools import *
class RedisTestCase(unittest.TestCase):
def setUp(self):
self.r = Redis(host='localhost', port=6379)
self.setup_sorted_sets()
self.setup_sets()
def tearDown(self):
self.r.zremrangebyrank('abc:win', 0, 100)
self.r.conn.flushdb()
self.r = None
def setup_sorted_sets(self):
self.r.zadd('abc:win', 5, 'liyuan', 7, 'mark', lilei=2, shicai=3)
def setup_sets(self):
self.r.sadd('tomongo', "James", "Mark", "Rendy", "Chris")
def test_connection_instance(self):
assert_is_instance(self.r.conn, redis.StrictRedis)
def test_ping_function_exists(self):
ping_exists = 'ping' in dir(self.r.conn)
assert_true(ping_exists)
def test_connection(self):
pong = self.r.conn.ping()
assert_true(pong)
def test_zadd_one(self):
number_of_new_entries = self.r.zadd('abc:win', 11, 'rendy')
assert_is_instance(number_of_new_entries, int)
eq_(number_of_new_entries, 1)
def test_zadd_multi(self):
number_of_new_entries = self.r.zadd('abc:win', 11, 'rendy', 12, 'mandy')
assert_is_instance(number_of_new_entries, int)
eq_(number_of_new_entries, 2)
def suite():
suite = unittest.TestSuite()
suite.addTest(RedisTestCase())
return suite
if __name__ == '__main__':
runner = unittest.TextTestRunner()
test_suite = suite()
runner.run(test_suite)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment