您现在的位置是:主页 > news > wordpress网站做成app/上海网络推广联盟

wordpress网站做成app/上海网络推广联盟

admin2025/5/24 0:31:34news

简介wordpress网站做成app,上海网络推广联盟,企业网络搭建及应用实验报告,小学校园门户网站建设方案TDD(Test Driven Development,测试驱动的开发)是软件开发史上最重要的里程碑之一。TDD主要专注于自动单元测试,它的目标是尽最大限度自动化测试代码。如果代码被改动,我们仍可以运行测试并捕捉可能存在的问题。换言之&…

wordpress网站做成app,上海网络推广联盟,企业网络搭建及应用实验报告,小学校园门户网站建设方案TDD(Test Driven Development,测试驱动的开发)是软件开发史上最重要的里程碑之一。TDD主要专注于自动单元测试,它的目标是尽最大限度自动化测试代码。如果代码被改动,我们仍可以运行测试并捕捉可能存在的问题。换言之&…

      TDDTest Driven Development,测试驱动的开发)是软件开发史上最重要的里程碑之一。TDD主要专注于自动单元测试,它的目标是尽最大限度自动化测试代码。如果代码被改动,我们仍可以运行测试并捕捉可能存在的问题。换言之,测试对于已经存在的功能模块依然有效。

        内容:

 单元测试;

 断言机制;

 浮点数精度。

1、断言函数

        单元测试通常使用断言函数作为测试的组成部分。在进行数值计算时,我们经常遇到比较两个近似相等的浮点数这样的基本问题。整数之间的比较很简单,但浮点数却非如此,这是由于计算机对浮点数的表示本身就是不精确的。numpy.testing包中有很多实用的工具函数考虑了浮点数比较的问题,可以测试前提是否成立。

        函 数 描 述:

assert_almost_equal 如果两个数字的近似程度没有达到指定精度,就抛出异常

assert_approx_equal 如果两个数字的近似程度没有达到指定有效数字,就抛出异常

assert_array_almost_equal 如果两个数组中元素的近似程度没有达到指定精度,就抛出异常

assert_array_equal 如果两个数组对象不相同,就抛出异常

assert_array_less 两个数组必须形状一致,并且第一个数组的元素严格小于第二个数组的元素,否则就抛出异常

assert_equal 如果两个对象不相同,就抛出异常

assert_raises 若用填写的参数调用函数没有抛出指定的异常,则测试不通过

assert_warns 若没有抛出指定的警告,则测试不通过

assert_string_equal 断言两个字符串变量完全相同

assert_allclose 如果两个对象的近似程度超出了指定的容差限,就抛出异常

 

 
  1. import numpy as np

  2. #使用NumPy testing包中的assert_almost_equal函数在不同的精度要求下检查了两个浮点数0.123456789和0.123456780是否近似相等

  3. # (1) 调用函数,指定较低的精度(小数点后7位):

  4. print 'Decimal 7',np.testing.assert_almost_equal(0.123456789,0.123456780,decimal=7)

  5. # (2) 调用函数,指定较高的精度(小数点后9位):

  6. print 'Decimal 9',np.testing.assert_almost_equal(0.123456789,0.123456780,decimal=9)

  7.  
  8. # (1) 调用函数,指定较低的有效数字位:

  9. print "Significance 8", np.testing.assert_approx_equal(0.123456789,0.123456780,significant=8)

  10. # (2) 调用函数,指定较高的有效数字位:

  11. print "Significance 9", np.testing.assert_approx_equal(0.123456789, 0.123456780,significant=9)

  12.  
  13. # (1) 调用函数,指定较低的精度:

  14. print "Decimal 8", np.testing.assert_array_almost_equal([0, 0.123456789], [0,0.123456780], decimal=8)

  15. # (2) 调用函数,指定较高的精度:

  16. print "Decimal 9", np.testing.assert_array_almost_equal([0, 0.123456789], [0,0.123456780], decimal=9)

  17.  
  18.  
  19. # (1) 调用assert_allclose函数:

  20. print "Pass", np.testing.assert_allclose([0, 0.123456789, np.nan], [0, 0.123456780,np.nan], rtol=1e-7, atol=0)

  21. # (2) 调用assert_array_equal函数:

  22. print "Fail", np.testing.assert_array_equal([0, 0.123456789, np.nan], [0, 0.123456780,np.nan])

  23.  
  24. # (1) 调用assert_array_less函数比较两个有严格顺序的数组:

  25. print "Pass", np.testing.assert_array_less([0, 0.123456789, np.nan], [1, 0.23456780,np.nan])

  26. # (2) 调用assert_array_less函数,使测试不通过:

  27. print "Fail", np.testing.assert_array_less([0, 0.123456789, np.nan], [0, 0.123456780,np.nan])

  28.  
  29. # 调用assert_equal函数:

  30. print "Equal?", np.testing.assert_equal((1, 2), (1, 3))

  31.  
  32. # (1) 调用assert_string_equal函数,比较一个字符串和其自身。显然,该测试应通过:

  33. print "Pass", np.testing.assert_string_equal("NumPy", "NumPy")

  34.  
  35. # (2) 调用assert_string_equal函数,比较一个字符串和另一个字母完全相同但大小写有区别的字符串。该测试应抛出异常:

  36. print "Fail", np.testing.assert_string_equal("NumPy", "Numpy")

  37.  
  38.  
  39. # (1) 使用finfo函数确定机器精度:

  40. eps = np.finfo(float).eps

  41. print "EPS", eps

  42.  
  43. # (2) 使用assert_array_almost_equal_nulp函数比较两个近似相等的浮点数1.0和1.0+ eps(epsilon),然后对1.0 + 2 * eps做同样的比较:

  44. print "1",

  45. np.testing.assert_array_almost_equal_nulp(1.0, 1.0 + eps)

  46. print "2",

  47. np.testing.assert_array_almost_equal_nulp(1.0, 1.0 + 2 * eps)

  48.  
  49. # (1) 使用finfo函数确定机器精度:

  50. eps = np.finfo(float).eps

  51. print "EPS", eps

  52.  
  53. # (2) 与前面的“动手实践”教程做相同的比较,但这里我们使用assert_array_max_ulp函数和适当的maxulp参数值:

  54. print "1", np.testing.assert_array_max_ulp(1.0, 1.0 + eps)

  55. print "2", np.testing.assert_array_max_ulp(1.0, 1 + 2 * eps, maxulp=2)


2、单元测试

        单元测试是对代码的一小部分进行自动化测试的单元,通常是一个函数或方法。Python中有用于单元测试的PyUnit API(Application Programming Interface,应用程序编程接口)。

 

 
  1. import numpy

  2. import unittest

  3.  
  4. def factorial(n):

  5. if n == 0:

  6. return 1

  7.  
  8. if n < 0:

  9. raise ValueError, "Don't be so negative"

  10.  
  11. return numpy.arange(1, n+1).cumprod()

  12.  
  13. class FactorialTest(unittest.TestCase):

  14. def test_factorial(self):

  15. # 对3的阶乘进行测试,应该能通过

  16. self.assertEqual(6, factorial(3)[-1])

  17. numpy.testing.assert_equal(numpy.array([1, 2, 6]), factorial(3))

  18.  
  19. def test_zero(self):

  20. # 对0的阶乘进行测试,应该能通过

  21. self.assertEqual(1, factorial(0))

  22.  
  23. def test_negative(self):

  24. # 对负整数的阶乘进行测试,应该不能通过

  25. # 阶乘函数会抛出一个ValueError类型的异常,但我们期望得到一个IndexError类型的异常

  26. self.assertRaises(IndexError, factorial(-10))

  27.  
  28. if __name__ == '__main__':

  29. unittest.main()

运行:

 

 
  1. .E.

  2. ======================================================================

  3. ERROR: test_negative (__main__.FactorialTest)

  4. ----------------------------------------------------------------------

  5. Traceback (most recent call last):

  6. File "C:/Users/Administrator/Desktop/Python_Opencv/Test/One/func.py", line 598, in test_negative

  7. self.assertRaises(IndexError, factorial(-10))

  8. File "C:/Users/Administrator/Desktop/Python_Opencv/Test/One/func.py", line 581, in factorial

  9. raise ValueError, "Don't be so negative"

  10. ValueError: Don't be so negative

  11.  
  12. ----------------------------------------------------------------------

  13. Ran 3 tests in 0.013s

  14.  
  15. FAILED (errors=1)

  16.  


nose 和测试装饰器:

       鼻子(nose)是长在嘴上方的器官,人和动物的呼吸和闻都依赖于它。nose同时也是一种Python框架,使得(单元)测试更加容易。nose可以帮助你组织测试代码。根据nose的文档,“任何能够匹配testMatch正则表达式(默认为(?:^|[b_.-])[Tt]est)的Python源代码文件、文件夹或库都将被收集用于测试”。nose充分利用了装饰器(decorator)。Python装饰器是有一定含义的对函数或方法的注解。numpy.testing模块中有很多装饰器。
装 饰 器 描 述

numpy.testing.decorators.deprecated 在运行测试时过滤掉过期警告

numpy.testing.decorators.knownfailureif 根据条件抛出KnownFailureTest异常

numpy.testing.decorators.setastest 将函数标记为测试函数或非测试函数

numpy.testing.decorators. skipif 根据条件抛出SkipTest异常

numpy.testing.decorators.slow 将测试函数标记为“运行缓慢”

参考:http://python.jobbole.com/81683/

            https://www.zhihu.com/question/26930016