学习Python的第一课(简单的单元测试)

由于有C#开发基础,感觉学习Python应该不难,主要是一些语法了,再加上现在互联网这么发达。

感觉还是要有思路,否则学什么也只能是什么。 话不多说,简单发下这几天的学习成果吧;

第一次写博客,大家不要见笑啊 简单的语法就不多说了,随便搜搜就可以得到。()

单元测试来了: 要做两个测试:

# 用于判断质数

import math

def isPrime(n):

  print ("验证数字"+str(n)+"是否质数开始")

  print ("开平方:"+str(round(math.sqrt(n))))

  if(n<=1):

    return False;

  for i in range(2,round(math.sqrt(n))):

#只需要判断到数的开平方数字即可

if(n % i==0): print (n/i)

return False;

return True;

# 用于判断一个12位的数字,最后一位是前11位除以7的余模

def isBarcode(n): print ("验证数字"+n+"是否符合规则开始")

  if(len(n)!=12):

    return False;

  ncount=int(n[0:11])#取前11位

  endNumber=int(n[-1]) #取最后1位

  print (ncount)

  print (endNumber)

  if(endNumber==ncount % 7):

    return True;

  else:

    print (ncount % 7)

  return False;

#这个文件保存为count.py。(名字随便起的了)

#下面开始验证 #保存为test.py

from count import isBarcode

from count import isPrime

import unittest

class Test(unittest.TestCase):

  print ("--Test--")

  def setUp(self):

  print ("--start--")

  #先来两个正确的号码

  def test_case(self):

    self.assertTrue(isBarcode("126112611262"),"it is not Barcode")

    self.assertTrue(isPrime(157),"it is 质数")

  def tearDown(self):

    print ("--End--")

print ("测试一下看显示在什么位置")

if __name__=="__main__":

unittest.main()  

在代码中我们看到有这样的一句代码:

if __name__=="__main__":

  unittest.main() 很有好奇的感觉,网上搜搜这是什么意思,

见 http://www.cnblogs.com/xuxm2007/archive/2010/08/04/1792463.html,感谢这位仁兄 读了几遍,终于读懂,果然很强! 说白了就是写一些代码,给自己用的,类似于private是不是?又担任着模块的入口(个人理解)。

看运行结果吧:

>>> ================================ RESTART ================================

>>> --unnitest:main模块

--Test--

测试一下看显示在什么位置

--start--

验证数字126112611262是否符合规则开始

12611261126

2 验证数字157是否质数开始

开平方:13

--End-- .

----------------------------------------------------------------------

Ran 1 test in 0.045s

OK

>>> #哈,说明验证通过了! 但是不知道大家是不是发现多了一个字符串"

--unnitest:main模块",这个是我自己加的了,开源的东西就是好,随便玩;

就在C:Python33Libunittest下面的main.py增加了一行代码:

from . import loader, runner

from .signals import installHandler

__unittest = True

print ("--unnitest:main模块")#这是新增加的

##好了,我们做个出错的单元测试吧,157改成156

结果就是这样的了:

>>> ================================ RESTART ================================

>>> --unnitest:main模块

--Test--

测试一下看显示在什么位置

--start--

验证数字126112611262是否符合规则开始

12611261126

2

验证数字156是否质数开始

开平方:12

78.0

--End--

F

======================================================================

FAIL: test_case (__main__.Test)

----------------------------------------------------------------------

Traceback (most recent call last): File "C:Python33 est.py", line 11, in test_case self.assertTrue(isPrime(156),"it is not 质数") AssertionError: False is not true : it is not 质数

----------------------------------------------------------------------

Ran 1 test in 0.052s FAILED (failures=1)

>>> #出错了!!!!!!!!156不是个质数啊;再来一次:这次把126112611262换成126112611261,156保持不变

>>> ================================ RESTART ================================

>>>

--unnitest:main模块

--Test--

测试一下看显示在什么位置

--start--

验证数字126112611261是否符合规则开始

12611261126

1

2

--End--

F

======================================================================

FAIL: test_case (__main__.Test)

----------------------------------------------------------------------

Traceback (most recent call last): File "C:Python33 est.py", line 10, in test_case self.assertTrue(isBarcode("126112611261"),"it is not Barcode") AssertionError: False is not true : it is not Barcode

##结束;呀呀呀,第一个提示之后第二个不执行啦?!(怎么回事?,可以做2个单元测试吗?求解

----------------------------------------------------------------------

Ran 1 test in 0.033s

FAILED (failures=1)

>>> #我用的IDLE (Python GUI),弹出提示比较慢,很多代码还是手敲;不过我感觉这也是它的魅力吧

#感谢虫师,正是参考了你的书才能模仿出这样的代码(关于selenium的),我学Python也是要做自动化测试(嘿嘿)

#很多东西还得进一步摸索,一些原理也不是很懂,还得加油!

#写在C罗的葡萄牙进4强的当天,马上就是比利时VS威尔士,希望贝尔可以和C罗来一场较量,哈哈。

原文地址:https://www.cnblogs.com/flyingtercel/p/5634497.html