内容简介:又是和单元测试有关的代码的阅读 :smile:unittest里的概念很简单,几乎和文件组织一致
又是和单元测试有关的代码的阅读 :smile:
unittest 源码以及注释
https://github.com/jiajunhuang/cpython/tree/annotation/Lib/unittest
简单讲解
unittest里的概念很简单,几乎和文件组织一致
tree . ├── case.py ├── __init__.py ├── loader.py ├── __main__.py ├── main.py ├── result.py ├── runner.py ├── signals.py ├── suite.py └── util.py
其中mock和unittest的test被我移除了。
-
case.py就是我们平时继承的unittest.TestCase所在处 -
loader.py加载测试用例 -
result.py保存结果的基类 -
runner.py实际跑单测的时候直接加载的TextTestResult和TextTestRunner所在地 -
signals.py处理相关信号 -
suite.pyTestSuite所在地,TestSuite是TestCase的集合
造个小轮子
# coding: utf-8
import importlib
import logging
class TestCase(object):
def __init__(self, name):
self.name = name
def setup(self):
pass
def teardown(self):
pass
class Loader(object):
def __init__(self):
self.cases = {}
def load(self, path):
module = importlib.import_module(path)
for test_class_name in dir(module):
test_class = getattr(module, test_class_name)
if (
isinstance(test_class, type) and
issubclass(test_class, TestCase)
):
self.cases.update({
test_class: self.find_test_method(test_class) or []
})
def find_test_method(self, test_class):
test_methods = []
for method in dir(test_class):
if method.startswith("test_"):
test_methods.append(
getattr(test_class, method)
)
return test_methods
def __iter__(self):
for test_class, test_cases in self.cases.items():
yield test_class, test_cases
class Runner(object):
def __init__(self, path):
self.path = path
def run(self):
loader = Loader()
loader.load(self.path)
for test_class, test_cases in loader:
test_instance = test_class(test_class.__name__)
test_instance.setup()
try:
for test_case in test_cases:
test_case(test_instance)
except:
logging.exception("error occured, skip this method")
test_instance.teardown()
用上面的 TestCase
写个测试用例:
from myunittest import TestCase
class DemoTestCase(TestCase):
def setup(self):
print("setup")
def teardown(self):
print("teardown")
def test_normal(self):
print("test normal function")
def test_exception(self):
raise Exception("haha, exception here!")
启动文件:
from myunittest import Runner
if __name__ == "__main__":
runner = Runner("test_demo")
runner.run()
执行结果:
$ python main.py
setup
ERROR:root:error occured, skip this method
Traceback (most recent call last):
File "/home/jiajun/Code/tests/myunittest/myunittest.py", line 64, in run
test_case(test_instance)
File "/home/jiajun/Code/tests/myunittest/test_demo.py", line 15, in test_exception
raise Exception("haha, exception here!")
Exception: haha, exception here!
teardown
以上所述就是小编给大家介绍的《unittest 源代码阅读》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 如何阅读源代码
- 如何阅读一份源代码?(2020 年版)
- 如何阅读Django源代码-上篇(the5fire版)
- 如何阅读Django源代码-上篇(the5fire版)
- 代码审计--源代码审计思路
- 开放源代码库指南
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
迎接互联网的明天
邹静 / 电子工业 / 2011-6 / 55.00元
《迎接互联网的明天-玩转3D Web(附盘)》,全书共5章,第1章主要阐述了国内外空前繁荣的3D互联网技术领域,以及这些领域透射出来的潜在商机;第2章主要用当下比较流行的Flash编程语言ActionScript 3,来向大家介绍面向对象编程语言的思想概念,以及一些3D渲染技术的入门知识;第3章注重建模知识的运用,主要运用WireFusion和3ds Max来制作3D网页;第4章主要介绍3D游戏编......一起来看看 《迎接互联网的明天》 这本书的介绍吧!