呼叫unittest.main()時無法傳入額外的引數給我們的測試類時,可以使用如下工具給我們的測試類新增額外的引數:
unittest_utils.py**:
import unittest
class parametrizedtestcase(unittest.testcase):
""" testcase classes that want to be parametrized should
inherit from this class.
"""def __init__(self, method_name='runtest', param=none):
super(parametrizedtestcase, self).__init__(method_name)
self.param = param
@staticmethod
def parametrize(test_case_class, param=none):
""" create a suite containing all tests taken from the given
subclass, passing them the parameter 'param'.
"""test_loader = unittest.testloader()
test_names = test_loader.gettestcasenames(test_case_class)
suite = unittest.testsuite()
for name in test_names:
suite.addtest(test_case_class(name, param=param))
return suite
使用時:
(1)需要讓測試類繼承自parametrizedtestcase
(2)新增額外引數
suite = unittest.testsuite()
suite.addtest(parametrizedtestcase.parametrize(testandroid, param=data))
runner = unittest.texttestrunner()
runner.run(suite)
run.py**:
import unittest
from multiprocessing import process
from demo.test_demo import testandroid
from utils.unittest_utils import parametrizedtestcase
class myprocess(process):
def __init__(self, data):
process.__init__(self)
self.data = data
self.suite = none
def run(self):
parametrizedtestcase.data = self.data
self.suite = unittest.testsuite()
# 此處param即為給測試類額外傳入的引數
self.suite.addtest(parametrizedtestcase.parametrize(testandroid, param=self.data))
runner = unittest.texttestrunner()
runner.run(self.suite)
def create_process_monkey(data):
process_instances =
for d in data:
instance = myprocess(d)
for instance in process_instances:
instance.start()
if __name__ == "__main__":
process01 =
process02 =
create_process_monkey([process01, process02])
logger.py**:
def add_log(func):
# 獲取用例名稱
case = func.__name__
print(case + " : start ...")
print("")
# 執行測試用例
func(*args)
print(case + " : end ...")
print("")
test_demo.py**示例:
import os
from utils.logger import add_log
from utils.unittest_utils import parametrizedtestcase
class testandroid(parametrizedtestcase):
def setup(self):
pass
# 格式化輸出裝飾器
@add_log
def test_calculate(self):
print("當前程序的pid是: " + str(os.getpid()))
self.asserttrue(eval(self.param["calculate"]) == int(self.param["value"]))
def teardown(self):
pass
執行結果:
多程序伺服器
基於tcp實現多程序伺服器 伺服器端 1 建立套接字 include include int socket int domain,int type,int protocol domain 乙個位址描述。目前僅支援af inet格式,也就是說arpa internet位址格式。type 指定socke...
多程序伺服器
注意 包含了 wrap.c 和 wrap.h 檔案在上篇部落格中 server.c include include include in.h include include include include include include wrap.h define maxline 8192 defi...
Python HTTP多程序伺服器
import socket import multiprocessing import re class server object def init self,port 在初始化中做好tcp連線的準備工作 1建立乙個tcp套接字 self.tcp socket socket.socket sock...