1.功能测试

常规测试

#常规测试代码,一个模块写功能,一个模块调用功能

#=============模块1:gongneng_ceshi
def func(v1, v2):
return v1* v2
#=============模块2
from gongneng_ceshi import func #导入模块1 def main():
if func(5,6) *3 ==30:
print("乘法计算ok")
else:
print("数学乘法计算失败!") def main2():
if func("hello",3) == "hellohellohello":
print("字符串乘法计算ok")
else:
print("字符串乘法计算失败!")
if __name__ == '__main__':
main()
main2()
"""
数学乘法计算失败!
字符串乘法计算ok
"""
常规的功能测试代码,结构简单,测试单一,测试比较分散

doctest模块,测试单个功能

import doctest  

#在程序中,对执行部分进行描述,结果部分直接编写
# 坑逼模块,>>> 后面必须留一个空格,不然会报错!!!
def fun(v1, v2):
"""
>>> fun(5,6)
30
>>> fun("he",2)
'hehe'
"""
return v1 * v2 if __name__ == '__main__':
doctest.testmod(verbose=True)
#True表示,执行时候输出详细信息,默认为False
"""
返回执行时间
Testing started at 10:32 ...
"""

unittest模块,测试单个文件

#功能模块1==============================
class Math:
def add(self, numa, numb):
return numa + numb
def sub(self, numa, numb):
return numa - numb if __name__ == '__main__':
m = Math()
#测试模块2==============================
from unitest_demo import Math import unittest class Testmath(unittest.TestCase): #继承testcase父类
@classmethod
def setUpClass(self):
print("【=======全部测试开始=====】")
@classmethod
def tearDownclass(self):
print("【=======全部测试结束=====】")
def tearDown(self) -> None:
print("{}:测试结束".format(self.id()))
def setUp(self) -> None:
print("{}:测试开始".format(self.id()))
def test_add(self):
self.assertEqual(Math().add(1,2),3) # 不需要测试的功能,使用装饰器进行装饰
@unittest.skip("Math.sub方法,功能暂不需要测试。")
def test_sub(self):
self.assertEqual(Math().sub(3,2),1)
if __name__ == '__main__':
unittest.main() """
============================= test session starts =============================
collecting ... collected 2 items
test_math.py::Testmath::test_add 【=======全部测试开始=====】
PASSED [ 50%]test_math.Testmath.test_add:测试开始
test_math.Testmath.test_add:测试结束
test_math.py::Testmath::test_sub SKIPPED (Math.sub方法,功能暂不需要...) [100%]
Skipped: Math.sub方法,功能暂不需要测试。
=================== 1 passed, 1 skipped, 1 warning in 0.04s ===================
Process finished with exit code 0
"""

unittest模块,测试多个文件

# 测试文件,都保存在某个目录下,可以集中测试全部文件
# 下面定义一个单独的模块脚本 import os
import unittest class RunAllTest(unittest.TestCase):
def test_run(self):
case_path = os.getcwd() # 获取测试文件目录
discover = unittest.defaultTestLoader.discover(case_path, pattern="test_ma*.py")
runner = unittest.TextTestRunner(verbosity=2)
runner.run(discover)
if __name__ == '__main__':
unittest.main() """
Testing started at 20:25 ...
Launching pytest with arguments test_file.py::RunAllTest --no-header --no-summary -q in E:\code\hunjia_16\day11_0824\unittest_demo
============================= test session starts =============================
collecting ... collected 1 item
test_file.py::RunAllTest::test_run PASSED [100%]【=======全部测试开始=====】
test_math.Testmath.test_add:测试开始
test_math.Testmath.test_add:测试结束
test_add (test_math.Testmath) ... ok
test_sub (test_math.Testmath) ... skipped 'Math.sub方法,功能暂不需要测试。'
----------------------------------------------------------------------
Ran 2 tests in 0.001s
OK (skipped=1)
======================== 1 passed, 1 warning in 0.01s =========================
"""

2.性能测试cProfile

#性能分析,profile,以及cprofile模块

import cProfile  #需要导入模块cProfile,注意P是大写
def plu(num):
s = 0
for i in range(num):
s += i
return s
if __name__ == '__main__':
# 测试plu功能函数,后面可定义结果保存位置,文件名【不定义位置,会直接输出】
cProfile.run("plu(9999999)", "f:\\test.result")
"""
ncalls tottime percall cumtime percall filename:lineno(function)
函数调用总次数 总运行时间 运行平均时间 总计运行时间 运行一次的平均时间 所在文件名,代码行,函数名 ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.542 0.542 <string>:1(<module>)
1 0.542 0.542 0.542 0.542 cprofile_demo.py:4(plu)
1 0.000 0.000 0.542 0.542 {built-in method builtins.exec}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} """

pstats报告的保存分析

import pstats  #需要先导入模块
def main():
stats = pstats.Stats( "f:\\test.result") #定义保存位置
stats.sort_stats("time") #按照时间排序
stats.print_stats() # 打印统计报告
if __name__ == '__main__':
main()
"""
Wed Aug 25 16:48:48 2021 f:\test.result
4 function calls in 0.589 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.589 0.589 0.589 0.589 E:/code/hunjia_16/day11_0824/xing_neng_ceshi/cprofile_demo.py:4(plu)
1 0.000 0.000 0.589 0.589 {built-in method builtins.exec}
1 0.000 0.000 0.589 0.589 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
"""

3.代码规范性检测

python默认是pep8的规范,pycharm就是遵守这个规范,Ctrl+Alt+L可以直接调整格式

pylint模块规范检测组件

#需要先安装pylint包
#方法1==================================
"""
1.编写一段代码
2.进入Terminal窗口
3.cd到当前脚本目录
4.pylint 文件名
pylint .\pylint_demo.py
5.会得到类似如下返回结果
************* Module pylint_demo
pylint_demo.py:4:0: C0305: Trailing newlines (trailing-newlines)
pylint_demo.py:1:0: C0114: Missing module docstring (missing-module-docstring)
pylint_demo.py:1:0: C0115: Missing class docstring (missing-class-docstring)
pylint_demo.py:2:4: C0116: Missing function or method docstring (missing-function-docstring)
pylint_demo.py:2:4: R0201: Method could be a function (no-self-use)
pylint_demo.py:1:0: R0903: Too few public methods (1/2) (too-few-public-methods) """
#方法2==================================
"""
1.进入pycharm
2.打开settings---Tools---External Tools---添加执行工具----配置相关信息
3.运行程序即可
"""

flake8模块规范检测组件

# 操作同上

Python入门-程序测试的更多相关文章

  1. Mahout学习之Mahout简介、安装、配置、入门程序测试

    一.Mahout简介 查了Mahout的中文意思——驭象的人,再看看Mahout的logo,好吧,想和小黄象happy地玩耍,得顺便陪陪这位驭象人耍耍了... 附logo: (就是他,骑在象头上的那个 ...

  2. Python入门-程序结构扩展

    deque双端队列 #双端队列,就是生产消费者模式,依赖collections模块 from collections import deque def main(): info = deque((&q ...

  3. Python入门-函数的使用到程序的公布安装

    Python入门-函数的使用到Python的公布安装 本文主要适合有一定编程经验,至少掌握一门编程语言的人查看. 文中样例大多都是简单到认识英文单词就能看懂的水平,主要讲的是Python的总体使用方法 ...

  4. Python入门(一):PTVS写Python程序,调试模式下input()提示文字乱码问题

    前两天写了Python入门(一),里面提到,使用VS2013+PTVS进行Python开发. 就在准备为第二篇写个demo的时候,发现了一个问题,各种解决无果,有些纠结 Python中输入函数是inp ...

  5. python web入门程序

    python2.x web入门程序 #!/usr/bin/python # -*- coding: UTF-8 -*- # 只在python2.x 有效 import os #Python的标准库中的 ...

  6. python入门(7)Python程序的风格

    python入门(7)Python程序的风格 Python采用缩进方式,写出来的代码就像下面的样子: # print absolute value of an integer: a = 100 if ...

  7. python入门(4)第一个python程序

    python入门(4)第一个python程序 在交互式环境的提示符>>>下,直接输入代码,按回车,就可以立刻得到代码执行结果.现在,试试输入100+200,看看计算结果是不是300: ...

  8. 怎么样通过编写Python小程序来统计测试脚本的关键字

    怎么样通过编写Python小程序来统计测试脚本的关键字 通常自动化测试项目到了一定的程序,编写的测试代码自然就会很多,如果很早已经编写的测试脚本现在某些基础函数.业务函数需要修改,那么势必要找出那些引 ...

  9. 40个Python入门小程序

    有不少同学学完Python后仍然很难将其灵活运用.我整理 37 个Python入门的小程序.在实践中应用Python会有事半功倍的效果. 分享 Github 项目,里面收集了 Python 学习资料 ...

随机推荐

  1. ubuntu 学习中的坑-2021-11-22

    安装ssh-server服务 查看是否安装ssh服务 #dpkg -l | grep ssh 安装ssh-server服务 #sudo apt-get install openssh-server 然 ...

  2. 1.1 STL基本概念

    文章目录 1 STL概述 1.1 STL基本概念 1.2 STL 六大组件 1.3 STL优点 2.1 容器 2.2 算法 2.3 迭代器 2.4 示例 1 STL概述 STL是StandardTem ...

  3. Casbin入选2022 Google编程之夏

    Casbin入选2022 Google编程之夏! Google编程之夏(Google Summer of Code,GSoC),是由Google公司所主办的年度开源程序设计项目,第一届从2005年开始 ...

  4. FreeBSD 利用IPFW实现限制局域网使用QQ

    QQ服务器分为三类: 1.UDP 8000端口类7个:速度最快,服务器最多.QQ上线会向这7个服务器发送UDP数据包,选择回复速度最快的一个作为连接服务器.这7个服务器名字均以sz-sz7开头,域后缀 ...

  5. spring——通过注解显式的完成自动装配

    构建bean文件: public class People { private String name = "小明"; } 编写配置类: @Configuration @Impor ...

  6. 自定义函数实现atoi功能

    思路: 列如char a[ ] ="123" "1" "2" "3' "\0" 首先遍历这个字符串 知道这个字 ...

  7. struts-032利用工具 PythonGUI

    # -*- coding: utf-8 -*- import requests from Tkinter import * class App: def __init__(self, master): ...

  8. 题解0005:数的划分(洛谷P1025)

    题目描述:将整数 n 分成 k 份,每份不能为空,颠倒顺序的被看成一种分法. 题目链接:https://www.luogu.com.cn/problem/P1025 题目思路:深搜剪枝,规定搜索的下一 ...

  9. C# XML基础入门(XML文件内容增删改查清)

    前言: 最近对接了一个第三方的项目,该项目的数据传输格式是XML.由于工作多年只有之前在医疗行业的时候有接触过少量数据格式是XML的接口,之后就几乎没有接触过了.因此对于XML这块自己感觉还是有很多盲 ...

  10. java LinkedList (详解)

    Java 链表(LinkedList) 一.链表简介 1.链表 (Linked List) 是一种常见的基础数据结构,是一种线性表,但是链表不会按线性表的顺序存储数据,而是每个节点里存到下一个节点的地 ...