- 浏览: 181165 次
- 性别:
- 来自: 武汉
-
文章分类
最新评论
-
beiizl:
用了博主的方法和代码,不同证书居然可以正常通讯?
Java SSLSocket的使用 -
SHANGLIJAVA:
sorry,运行时没看清。博主的代码确实没问题。。。
Java SSLSocket的使用 -
SHANGLIJAVA:
YoungeeOne 写道最后一个为什么初始化一个空的证书,也 ...
Java SSLSocket的使用 -
q979713444:
那这个的心跳怎么弄呢
Java SSLSocket的使用 -
43350860:
busybox不是每台机器有安装的, 有没有比较裸的办法获取p ...
android中查看端口占用
使用python unittest做测试
http://www.cnblogs.com/imouren/archive/2011/08/04/2127997.html
python unittest单元测试
http://catmic27.blog.51cto.com/2517040/946852
# coding: utf-8 class Area: def __init__(self, width=100, height=100): self._width = width self._height = height def get_width(self): return self._width def get_height(self): return self._height def get_area(self): return self._width * self._height def set_width(self, width): if width <= 0: raise ValueError, "Illegal width value" self._width = width def set_height(self, height): if height <= 0: raise ValueError, "Illegal height value" self._height = height import unittest class AreaTest(unittest.TestCase): def setUp(self): self.area = Area() def tearDown(self): self.area = None def test_area(self): self.assertEqual(self.area.get_area(), 100 * 100) def test_width(self): self.area.set_width(1) self.assertEqual(self.area.get_area(), 1 * 100) if __name__ == '__main__': unittest.main()
unittest.TestCase有如下用于测试的方法
assertAlmostEqual assertAlmostEquals assertEqual assertEquals assertFalse assertNotAlmostEqual assertNotAlmostEquals assertNotEqual assertNotEquals assertRaises assertTrue assert_ countTestCases debug defaultTestResult fail failIf failIfAlmostEqual failIfEqual failUnless failUnlessAlmostEqual failUnlessEqual failUnlessRaises failureException id run setUp shortDescription tearDown
# coding: utf-8 class SampleClass: """ >>> print 1 1 >>> # comments get ignored. so are empty PS1 and PS2 prompts: >>> ... Multiline example: >>> sc = SampleClass(3) >>> for i in range(10): ... sc = sc.double() ... print sc.get(), 6 12 24 48 96 192 384 768 1536 3072 """ def __init__(self, val): """ >>> print SampleClass(12).get() 12 """ self.val = val def get(self): """ >>> print SampleClass(3).get() 3 """ return self.val def double(self): """ >>> print SampleClass(10).double().get() 20 """ return SampleClass(self.val + self.val) def a_staticmethod(val): """ 这个静态方法将传入的值加1并返回 >>> print SampleClass.a_staticmethod(10) 11 """ return val + 1 a_staticmethod = staticmethod(a_staticmethod) def a_classmethod(cls, val): """ 这个类方法将传入的值加12并返回 >>> print SampleClass.a_classmethod(100) 112 >>> print SampleClass(0).a_classmethod(0) 12 """ return val + 12 a_classmethod = classmethod(a_classmethod) a_property = property(get, doc=""" >>> print SampleClass(22).a_property 22 """) class NestedClass: """ 这个嵌套类对val进行平方求值 >>> x = SampleClass.NestedClass(5) >>> y = x.square() >>> print y.get() 25 """ def __init__(self, val=0): """ """ self.val = val def square(self): return SampleClass.NestedClass(self.val * self.val) def get(self): return self.val def sample_func(v): """ Blah blah >>> print sample_func(22) 44 >>> print sample_func(0) 0 >>> print sample_func(-22) -44 Yee ha! """ return v+v """ 这是一个使用doctest进行测试的小例子. 主要测试factorial 是否返回阶乘 >>> factorial(5) 120 """ def factorial(n): """Return the factorial of n, an exact integer >= 0. If the result is small enough to fit in an int, return an int. Else return a long. >>> [factorial(n) for n in range(6)] [1, 1, 2, 6, 24, 120] >>> [factorial(long(n)) for n in range(6)] [1, 1, 2, 6, 24, 120] >>> factorial(30) 265252859812191058636308480000000L >>> factorial(30L) 265252859812191058636308480000000L """ result = 1 factor = 2 while factor <= n: result *= factor factor += 1 return result if __name__ == '__main__': import doctest doctest.testmod()
# -*- coding: utf-8 -*- import doctest class SampleNewStyleClass(object): r""" 打印1, 2, 3, 中间有换行 >>> print '1\n2\n3' 1 2 3 """ def __init__(self, val): """ """ self.val = val def double(self): """ 这个方法返回一个新的SampleNewStyleClass对象, 其值为原来的2倍 >>> print SampleNewStyleClass(23).double().get() 46 """ return SampleNewStyleClass(self.val + self.val) def get(self): """ 这个方法返回val值 >>> print SampleNewStyleClass(25).get() 25 """ return self.val if __name__ == '__main__': doctest.testmod()
def test_DocTestSuite(): """DocTestSuite creates a unittest test suite from a doctest. We create a Suite by providing a module. A module can be provided by passing a module object: >>> import unittest >>> import test.sample_doctest >>> suite = doctest.DocTestSuite(test.sample_doctest) >>> suite.run(unittest.TestResult()) <unittest.result.TestResult run=9 errors=0 failures=4> We can also supply the module by name: >>> suite = doctest.DocTestSuite('test.sample_doctest') >>> suite.run(unittest.TestResult()) <unittest.result.TestResult run=9 errors=0 failures=4> We can use the current module: >>> suite = test.sample_doctest.test_suite() >>> suite.run(unittest.TestResult()) <unittest.result.TestResult run=9 errors=0 failures=4> We can supply global variables. If we pass globs, they will be used instead of the module globals. Here we'll pass an empty globals, triggering an extra error: >>> suite = doctest.DocTestSuite('test.sample_doctest', globs={}) >>> suite.run(unittest.TestResult()) <unittest.result.TestResult run=9 errors=0 failures=5> Alternatively, we can provide extra globals. Here we'll make an error go away by providing an extra global variable: >>> suite = doctest.DocTestSuite('test.sample_doctest', ... extraglobs={'y': 1}) >>> suite.run(unittest.TestResult()) <unittest.result.TestResult run=9 errors=0 failures=3> You can pass option flags. Here we'll cause an extra error by disabling the blank-line feature: >>> suite = doctest.DocTestSuite('test.sample_doctest', ... optionflags=doctest.DONT_ACCEPT_BLANKLINE) >>> suite.run(unittest.TestResult()) <unittest.result.TestResult run=9 errors=0 failures=5> You can supply setUp and tearDown functions: >>> def setUp(t): ... import test.test_doctest ... test.test_doctest.sillySetup = True >>> def tearDown(t): ... import test.test_doctest ... del test.test_doctest.sillySetup Here, we installed a silly variable that the test expects: >>> suite = doctest.DocTestSuite('test.sample_doctest', ... setUp=setUp, tearDown=tearDown) >>> suite.run(unittest.TestResult()) <unittest.result.TestResult run=9 errors=0 failures=3> But the tearDown restores sanity: >>> import test.test_doctest >>> test.test_doctest.sillySetup Traceback (most recent call last): ... AttributeError: 'module' object has no attribute 'sillySetup' The setUp and tearDown funtions are passed test objects. Here we'll use the setUp function to supply the missing variable y: >>> def setUp(test): ... test.globs['y'] = 1 >>> suite = doctest.DocTestSuite('test.sample_doctest', setUp=setUp) >>> suite.run(unittest.TestResult()) <unittest.result.TestResult run=9 errors=0 failures=3> Here, we didn't need to use a tearDown function because we modified the test globals, which are a copy of the sample_doctest module dictionary. The test globals are automatically cleared for us after a test. """ def test_DocFileSuite(): """We can test tests found in text files using a DocFileSuite. We create a suite by providing the names of one or more text files that include examples: >>> import unittest >>> suite = doctest.DocFileSuite('test_doctest.txt', ... 'test_doctest2.txt', ... 'test_doctest4.txt') >>> suite.run(unittest.TestResult()) <unittest.result.TestResult run=3 errors=0 failures=3> The test files are looked for in the directory containing the calling module. A package keyword argument can be provided to specify a different relative location. >>> import unittest >>> suite = doctest.DocFileSuite('test_doctest.txt', ... 'test_doctest2.txt', ... 'test_doctest4.txt', ... package='test') >>> suite.run(unittest.TestResult()) <unittest.result.TestResult run=3 errors=0 failures=3> Support for using a package's __loader__.get_data() is also provided. >>> import unittest, pkgutil, test >>> added_loader = False >>> if not hasattr(test, '__loader__'): ... test.__loader__ = pkgutil.get_loader(test) ... added_loader = True >>> try: ... suite = doctest.DocFileSuite('test_doctest.txt', ... 'test_doctest2.txt', ... 'test_doctest4.txt', ... package='test') ... suite.run(unittest.TestResult()) ... finally: ... if added_loader: ... del test.__loader__ <unittest.result.TestResult run=3 errors=0 failures=3> '/' should be used as a path separator. It will be converted to a native separator at run time: >>> suite = doctest.DocFileSuite('../test/test_doctest.txt') >>> suite.run(unittest.TestResult()) <unittest.result.TestResult run=1 errors=0 failures=1> If DocFileSuite is used from an interactive session, then files are resolved relative to the directory of sys.argv[0]: >>> import types, os.path, test.test_doctest >>> save_argv = sys.argv >>> sys.argv = [test.test_doctest.__file__] >>> suite = doctest.DocFileSuite('test_doctest.txt', ... package=types.ModuleType('__main__')) >>> sys.argv = save_argv By setting `module_relative=False`, os-specific paths may be used (including absolute paths and paths relative to the working directory): >>> # Get the absolute path of the test package. >>> test_doctest_path = os.path.abspath(test.test_doctest.__file__) >>> test_pkg_path = os.path.split(test_doctest_path)[0] >>> # Use it to find the absolute path of test_doctest.txt. >>> test_file = os.path.join(test_pkg_path, 'test_doctest.txt') >>> suite = doctest.DocFileSuite(test_file, module_relative=False) >>> suite.run(unittest.TestResult()) <unittest.result.TestResult run=1 errors=0 failures=1> It is an error to specify `package` when `module_relative=False`: >>> suite = doctest.DocFileSuite(test_file, module_relative=False, ... package='test') Traceback (most recent call last): ValueError: Package may only be specified for module-relative paths. You can specify initial global variables: >>> suite = doctest.DocFileSuite('test_doctest.txt', ... 'test_doctest2.txt', ... 'test_doctest4.txt', ... globs={'favorite_color': 'blue'}) >>> suite.run(unittest.TestResult()) <unittest.result.TestResult run=3 errors=0 failures=2> In this case, we supplied a missing favorite color. You can provide doctest options: >>> suite = doctest.DocFileSuite('test_doctest.txt', ... 'test_doctest2.txt', ... 'test_doctest4.txt', ... optionflags=doctest.DONT_ACCEPT_BLANKLINE, ... globs={'favorite_color': 'blue'}) >>> suite.run(unittest.TestResult()) <unittest.result.TestResult run=3 errors=0 failures=3> And, you can provide setUp and tearDown functions: >>> def setUp(t): ... import test.test_doctest ... test.test_doctest.sillySetup = True >>> def tearDown(t): ... import test.test_doctest ... del test.test_doctest.sillySetup Here, we installed a silly variable that the test expects: >>> suite = doctest.DocFileSuite('test_doctest.txt', ... 'test_doctest2.txt', ... 'test_doctest4.txt', ... setUp=setUp, tearDown=tearDown) >>> suite.run(unittest.TestResult()) <unittest.result.TestResult run=3 errors=0 failures=2> But the tearDown restores sanity: >>> import test.test_doctest >>> test.test_doctest.sillySetup Traceback (most recent call last): ... AttributeError: 'module' object has no attribute 'sillySetup' The setUp and tearDown funtions are passed test objects. Here, we'll use a setUp function to set the favorite color in test_doctest.txt: >>> def setUp(test): ... test.globs['favorite_color'] = 'blue' >>> suite = doctest.DocFileSuite('test_doctest.txt', setUp=setUp) >>> suite.run(unittest.TestResult()) <unittest.result.TestResult run=1 errors=0 failures=0> Here, we didn't need to use a tearDown function because we modified the test globals. The test globals are automatically cleared for us after a test. Tests in a file run using `DocFileSuite` can also access the `__file__` global, which is set to the name of the file containing the tests: >>> suite = doctest.DocFileSuite('test_doctest3.txt') >>> suite.run(unittest.TestResult()) <unittest.result.TestResult run=1 errors=0 failures=0> If the tests contain non-ASCII characters, we have to specify which encoding the file is encoded with. We do so by using the `encoding` parameter: >>> suite = doctest.DocFileSuite('test_doctest.txt', ... 'test_doctest2.txt', ... 'test_doctest4.txt', ... encoding='utf-8') >>> suite.run(unittest.TestResult()) <unittest.result.TestResult run=3 errors=0 failures=2>
发表评论
-
python中的with用法简介
2013-01-01 11:00 0#! /usr/bin/python # enco ... -
(正式)Python学习及资源
2012-12-17 20:13 0python程序计时 http://www.cnblogs. ... -
开发工具收集
2012-12-15 10:17 0eclipse插件 http://developer. ... -
(正式)Java之JUnit, Log4J, Ant, HttpClient, ApacheCommons
2012-11-29 09:57 01. JUnit JUnit和EasyMock Unit ... -
python进行文件夹复制和jar包的合并
2012-10-28 20:21 0http://desert3.iteye.com/blog/7 ... -
python ui--好玩而已
2012-09-17 20:29 0http://forum.ubuntu.org.cn/view ... -
c/c++学习之mutest
2012-08-14 11:22 0最近使用mutest进行单元测试时发现无法找到testcase ... -
Java学习之JUnit(2)
2012-08-03 16:56 0JUnit4的参数化测试 http://yinhe2726. ... -
Java学习之JUnit
2012-08-03 14:51 0参考 http://www.cnblogs.com/eggb ... -
python小技巧
2012-08-02 21:28 0通过Python获取Mac地址 def get_mac ... -
工作积累(6)-使用python进行log分析
2012-08-02 17:38 02012-08-08 主要是使用python分析待机日志中的 ... -
工作积累(4)-如何加载assets下的html网页和testAccessAllowFileAccess cts测试失败
2012-07-30 13:54 01. 按照file:///android_asset/ ... -
web.py建站(4)-div居中以及字体颜色
2012-07-23 15:35 0div style常用属性 http://www.c ... -
web.py建站(4)-jquery
2012-07-23 12:23 0jquery选择器 http://www.cnblogs.c ... -
web.py建站(4)-如何操作sqlite
2012-07-21 09:40 0python sqlite http://askandstu ... -
web.py建站(3)-获取get请求或post请求参数
2012-07-20 17:25 0通过web.input()获取各个请求参数; 通过web.d ... -
web.py建站(2)-template传递参数
2012-07-20 17:05 0(转) http://bbs.python520.com/ar ... -
web.py建站(1)-备忘
2012-07-20 17:03 0web.py与jquery的$冲突 使用$$代替$ ... -
python socket
2012-07-06 15:45 0server端 import socket impo ... -
快速排序(2)
2012-07-06 14:02 0一个Quicksort究竟可以写到多么短 http: ...
相关推荐
内容概要:本文详细介绍了基于MATLAB GUI界面和卷积神经网络(CNN)的模糊车牌识别系统。该系统旨在解决现实中车牌因模糊不清导致识别困难的问题。文中阐述了整个流程的关键步骤,包括图像的模糊还原、灰度化、阈值化、边缘检测、孔洞填充、形态学操作、滤波操作、车牌定位、字符分割以及最终的字符识别。通过使用维纳滤波或最小二乘法约束滤波进行模糊还原,再利用CNN的强大特征提取能力完成字符分类。此外,还特别强调了MATLAB GUI界面的设计,使得用户能直观便捷地操作整个系统。 适合人群:对图像处理和深度学习感兴趣的科研人员、高校学生及从事相关领域的工程师。 使用场景及目标:适用于交通管理、智能停车场等领域,用于提升车牌识别的准确性和效率,特别是在面对模糊车牌时的表现。 其他说明:文中提供了部分关键代码片段作为参考,并对实验结果进行了详细的分析,展示了系统在不同环境下的表现情况及其潜在的应用前景。
嵌入式八股文面试题库资料知识宝典-计算机专业试题.zip
嵌入式八股文面试题库资料知识宝典-C and C++ normal interview_3.zip
内容概要:本文深入探讨了一款额定功率为4kW的开关磁阻电机,详细介绍了其性能参数如额定功率、转速、效率、输出转矩和脉动率等。同时,文章还展示了利用RMxprt、Maxwell 2D和3D模型对该电机进行仿真的方法和技术,通过外电路分析进一步研究其电气性能和动态响应特性。最后,文章提供了基于RMxprt模型的MATLAB仿真代码示例,帮助读者理解电机的工作原理及其性能特点。 适合人群:从事电机设计、工业自动化领域的工程师和技术人员,尤其是对开关磁阻电机感兴趣的科研工作者。 使用场景及目标:适用于希望深入了解开关磁阻电机特性和建模技术的研究人员,在新产品开发或现有产品改进时作为参考资料。 其他说明:文中提供的代码示例仅用于演示目的,实际操作时需根据所用软件的具体情况进行适当修改。
少儿编程scratch项目源代码文件案例素材-剑客冲刺.zip
少儿编程scratch项目源代码文件案例素材-几何冲刺 转瞬即逝.zip
内容概要:本文详细介绍了基于PID控制器的四象限直流电机速度驱动控制系统仿真模型及其永磁直流电机(PMDC)转速控制模型。首先阐述了PID控制器的工作原理,即通过对系统误差的比例、积分和微分运算来调整电机的驱动信号,从而实现转速的精确控制。接着讨论了如何利用PID控制器使有刷PMDC电机在四个象限中精确跟踪参考速度,并展示了仿真模型在应对快速负载扰动时的有效性和稳定性。最后,提供了Simulink仿真模型和详细的Word模型说明文档,帮助读者理解和调整PID控制器参数,以达到最佳控制效果。 适合人群:从事电力电子与电机控制领域的研究人员和技术人员,尤其是对四象限直流电机速度驱动控制系统感兴趣的读者。 使用场景及目标:适用于需要深入了解和掌握四象限直流电机速度驱动控制系统设计与实现的研究人员和技术人员。目标是在实际项目中能够运用PID控制器实现电机转速的精确控制,并提高系统的稳定性和抗干扰能力。 其他说明:文中引用了多篇相关领域的权威文献,确保了理论依据的可靠性和实用性。此外,提供的Simulink模型和Word文档有助于读者更好地理解和实践所介绍的内容。
嵌入式八股文面试题库资料知识宝典-2013年海康威视校园招聘嵌入式开发笔试题.zip
少儿编程scratch项目源代码文件案例素材-驾驶通关.zip
小区开放对周边道路通行能力影响的研究.pdf
内容概要:本文探讨了冷链物流车辆路径优化问题,特别是如何通过NSGA-2遗传算法和软硬时间窗策略来实现高效、环保和高客户满意度的路径规划。文中介绍了冷链物流的特点及其重要性,提出了软时间窗概念,允许一定的配送时间弹性,同时考虑碳排放成本,以达到绿色物流的目的。此外,还讨论了如何将客户满意度作为路径优化的重要评价标准之一。最后,通过一段简化的Python代码展示了遗传算法的应用。 适合人群:从事物流管理、冷链物流运营的专业人士,以及对遗传算法和路径优化感兴趣的科研人员和技术开发者。 使用场景及目标:适用于冷链物流企业,旨在优化配送路线,降低运营成本,减少碳排放,提升客户满意度。目标是帮助企业实现绿色、高效的物流配送系统。 其他说明:文中提供的代码仅为示意,实际应用需根据具体情况调整参数设置和模型构建。
少儿编程scratch项目源代码文件案例素材-恐怖矿井.zip
内容概要:本文详细介绍了基于STM32F030的无刷电机控制方案,重点在于高压FOC(磁场定向控制)技术和滑膜无感FOC的应用。该方案实现了过载、过欠压、堵转等多种保护机制,并提供了完整的源码、原理图和PCB设计。文中展示了关键代码片段,如滑膜观测器和电流环处理,以及保护机制的具体实现方法。此外,还提到了方案的移植要点和实际测试效果,确保系统的稳定性和高效性。 适合人群:嵌入式系统开发者、电机控制系统工程师、硬件工程师。 使用场景及目标:适用于需要高性能无刷电机控制的应用场景,如工业自动化设备、无人机、电动工具等。目标是提供一种成熟的、经过验证的无刷电机控制方案,帮助开发者快速实现并优化电机控制性能。 其他说明:提供的资料包括详细的原理图、PCB设计文件、源码及测试视频,方便开发者进行学习和应用。
基于有限体积法Godunov格式的管道泄漏检测模型研究.pdf
嵌入式八股文面试题库资料知识宝典-CC++笔试题-深圳有为(2019.2.28)1.zip
少儿编程scratch项目源代码文件案例素材-几何冲刺 V1.5.zip
Android系统开发_Linux内核配置_USB-HID设备模拟_通过root权限将Android设备转换为全功能USB键盘的项目实现_该项目需要内核支持configFS文件系统
C# WPF - LiveCharts Project
少儿编程scratch项目源代码文件案例素材-恐怖叉子 动画.zip
嵌入式八股文面试题库资料知识宝典-嵌⼊式⼯程师⾯试⾼频问题.zip