前言
最近一直出差很久没有更新博客了。刚好应朋友的需求需要了解学习python的测试用例入坑之旅。我先默默替他祈祷一下。
其实python是一个功能非常强大的脚本语言。爬虫、服务器等等有时候你会感觉它无所不能。
从2017年GitHub的程序语言排行榜可以看出而如今python在崛起啊。公司的很多python大神请先收下我的膝盖。
好了废话少说吧,下面让我这个菜鸟给大家简单介绍一个我去年写的一个灰常简单的测试用例吧。
测试用例有两个最最基本的操作:连接服务器请求数据、连接后台DB修改数据。
1、连接服务器请求数据
# _*_ coding:utf-8 _*_
__author__ = 'Patrick'
import simplejson
import socket
import sys
if __name__ == '__main__':
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect(('localhost', 8080))
except:
print "connect error"
sys.exit()
dic = {"1": 1, "2": 2}
print 'dic type ', type(dic)
st = simplejson.dumps(dic)
print 'after dumps ',type(st)
sock.send(st)
sock.close()
当然真正连接服务器肯定还有其他自己额外的操作,这个就需要自己的需求自己定义了,包括收发数据格式等问题。
2.连接数据库
请参考这篇文章Python(Pycharm)连接Oracle数据库之cx_Oracle的使用
其实python功能特别多,也特别强大。很多东西都需要针对具体的需求去写代码。下面可以给各位附上简单的不连接服务器和数据库的测试用例
import unittest
class HelloWorld(unittest):
def test_helloWorld(self):
print "Hello World!"
if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(HelloWorld("test_helloWorld"))
unittest.TextTestRunner().run(suite)
测试用例重要的是继承unittest类。