Pytest编写测试函数

Pytest编写测试函数

一个规范的测试方法一定带有断言,在使用pytest时,可以直接使用Python自带的assert关键字

Pytest允许在assert关键字添加任意表达式,表达式的值通过bool转换后等于False则表示断言失败,测试用例执行失败;如果等于True则表示断言成功,测试用例执行成功。

重写assertpytest

可以重写assert关键字,它可以截断对原生assert的调用,替换为pytest定义的assert,从而展示更详细的信息和细节。

from collections import namedtuple
Task = namedtuple(‘Task‘, [‘summary‘,‘owner‘,‘done‘,‘id‘])
# __new__.__defaults__创建默认的Task对象
Task.__new__.__defaults__ = (None, None, False, None)

import pytest

def test_task_equality():
    t1 = Task(‘sit there‘, ‘brain‘)
    t2 = Task(‘do something‘, ‘okken‘)
    print(t1)
    print(t2)
    assert t1 == t2

def test_dict_equal():
    t1_dict = Task(‘make sandwich‘, ‘okken‘)._asdict()
    t2_dict = Task(‘make sandwich‘, ‘okkem‘)._asdict()
    print(t1_dict)
    print(t2_dict)
    assert t1_dict == t2_dict

执行结果如下:

E:\Programs\Python\Python_Pytest\TestScripts>pytest test_three.py
======================= test session starts ==========================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: xdist-1.29.0, timeout-1.3.3, repeat-0.8.0, instafail-0.4.1, forked-1.0.2, emoji-0.2.0, allure-pytest-2.6.3
collected 2 items                                                                                                                                                          

test_three.py FF                                                                                                                                                     [100%]

===================== FAILURES ==================================
_____________________ test_task_equality _______________________________

    def test_task_equality():
        t1 = Task(‘sit there‘, ‘brain‘)
        t2 = Task(‘do something‘, ‘okken‘)
        print(t1)
        print(t2)
>       assert t1 == t2
E       AssertionError: assert Task(summary=...alse, id=None) == Task(summary=‘...alse, id=None)
E         At index 0 diff: ‘sit there‘ != ‘do something‘
E         Use -v to get the full diff

test_three.py:14: AssertionError
-------------------- Captured stdout call ----------------------------------------
Task(summary=‘sit there‘, owner=‘brain‘, done=False, id=None)
Task(summary=‘do something‘, owner=‘okken‘, done=False, id=None)
________________ test_dict_equal ______________________________

    def test_dict_equal():
        t1_dict = Task(‘make sandwich‘, ‘okken‘)._asdict()
        t2_dict = Task(‘make sandwich‘, ‘okkem‘)._asdict()
        print(t1_dict)
        print(t2_dict)
>       assert t1_dict == t2_dict
E       AssertionError: assert OrderedDict([...(‘id‘, None)]) == OrderedDict([(...(‘id‘, None)])
E         Omitting 3 identical items, use -vv to show
E         Differing items:
E         {‘owner‘: ‘okken‘} != {‘owner‘: ‘okkem‘}
E         Use -v to get the full diff

test_three.py:22: AssertionError
------------------------------- Captured stdout call --------------------------------------------
OrderedDict([(‘summary‘, ‘make sandwich‘), (‘owner‘, ‘okken‘), (‘done‘, False), (‘id‘, None)])
OrderedDict([(‘summary‘, ‘make sandwich‘), (‘owner‘, ‘okkem‘), (‘done‘, False), (‘id‘, None)])
================ 2 failed in 0.20 seconds=============================

加上参数-v,执行结果如下:

E:\Programs\Python\Python_Pytest\TestScripts>pytest test_three.py -v
================= test session starts ===============================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0 -- c:\python37\python.exe
cachedir: .pytest_cache
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: xdist-1.29.0, timeout-1.3.3, repeat-0.8.0, instafail-0.4.1, forked-1.0.2, emoji-0.2.0, allure-pytest-2.6.3
collected 2 items                                                                                                                                                          

test_three.py::test_task_equality FAILED                                                                                                                             [ 50%]
test_three.py::test_dict_equal FAILED                                                                                                                                [100%]

==================== FAILURES =========================
____________________ test_task_equality ________________________

    def test_task_equality():
        t1 = Task(‘sit there‘, ‘brain‘)
        t2 = Task(‘do something‘, ‘okken‘)
        print(t1)
        print(t2)
>       assert t1 == t2
E       AssertionError: assert Task(summary=...alse, id=None) == Task(summary=‘...alse, id=None)
E         At index 0 diff: ‘sit there‘ != ‘do something‘
E         Full diff:
E         - Task(summary=‘sit there‘, owner=‘brain‘, done=False, id=None)
E         ?                ^^^  ^^^          ^^^^
E         + Task(summary=‘do something‘, owner=‘okken‘, done=False, id=None)
E         ?               +++ ^^^  ^^^          ^^^^

test_three.py:14: AssertionError
-------------------- Captured stdout call -----------------------------------
Task(summary=‘sit there‘, owner=‘brain‘, done=False, id=None)
Task(summary=‘do something‘, owner=‘okken‘, done=False, id=None)
____________________ test_dict_equal ________________________________

    def test_dict_equal():
        t1_dict = Task(‘make sandwich‘, ‘okken‘)._asdict()
        t2_dict = Task(‘make sandwich‘, ‘okkem‘)._asdict()
        print(t1_dict)
        print(t2_dict)
>       assert t1_dict == t2_dict
E       AssertionError: assert OrderedDict([...(‘id‘, None)]) == OrderedDict([(...(‘id‘, None)])
E         Omitting 3 identical items, use -vv to show
E         Differing items:
E         {‘owner‘: ‘okken‘} != {‘owner‘: ‘okkem‘}
E         Full diff:
E         OrderedDict([(‘summary‘, ‘make sandwich‘),
E         -              (‘owner‘, ‘okken‘),
E         ?                             ^...
E
E         ...Full output truncated (5 lines hidden), use ‘-vv‘ to show

test_three.py:22: AssertionError
--------------------------- Captured stdout call --------------------------------
OrderedDict([(‘summary‘, ‘make sandwich‘), (‘owner‘, ‘okken‘), (‘done‘, False), (‘id‘, None)])
OrderedDict([(‘summary‘, ‘make sandwich‘), (‘owner‘, ‘okkem‘), (‘done‘, False), (‘id‘, None)])
================= 2 failed in 0.13 seconds ============================

预期异常

"""
在Task项目的API中,有几个地方可能抛出异常
def add(task):  # type:(Task) ->int
def get(task_id):  # type:(int) ->Task
def list_tasks(owner=None):  # type:(str|None) ->list of task
def count():  # type:(None) ->int
def update(task_id, task):  # type:(int, Task) ->None
def delete(task_id):  # type:(int) ->None
def delete_all():  # type:() ->None
def unique_id():  # type:() ->int
def start_tasks_db(db_path, db_type):  # type:(str, str) ->None
def stop_tasks_db():  # type:() ->None
"""

import pytest
import tasks

def test_add_raises():
    with pytest.raises(TypeError):
        tasks.add(task=‘no a Task object‘)

"""
测试用例中有with pytest.raise(TypeError)生命,意味着无论with结构中的内容是什么
都至少会发生TypeError异常,如果测试通过,说明确实发生了我们预期TypeError,如果抛出的是其他类型的异常
则与我们预期的异常不一致,测试用例执行失败
"""

def test_start_tasks_db_raises():
    with pytest.raises(ValueError) as excinfo:
        tasks.start_tasks_db(‘some/great/path‘, ‘mysql‘)
    exception_msg = excinfo.value.args[0]
    assert exception_msg == "db_type must be a ‘tiny‘ or ‘mongo‘ "
    

测试函数的标记

from collections import namedtuple
import pytest
Task = namedtuple(‘Task‘, [‘summary‘,‘owner‘,‘done‘,‘id‘])
# __new__.__defaults__创建默认的Task对象
Task.__new__.__defaults__ = (None, None, False, None)

@pytest.mark.smoke
def test_task_equality():
    t1 = Task(‘sit there‘, ‘brain‘)
    t2 = Task(‘do something‘, ‘okken‘)
    print(t1)
    print(t2)
    assert t1 == t2

@pytest.mark.dict
@pytest.mark.smoke
def test_dict_equal():
    t1_dict = Task(‘make sandwich‘, ‘okken‘)._asdict()
    t2_dict = Task(‘make sandwich‘, ‘okkem‘)._asdict()
    print(t1_dict)
    print(t2_dict)
    assert t1_dict == t2_dict

执行结果如下:

E:\Programs\Python\Python_Pytest\TestScripts>pytest -v -m ‘smoke‘ test_five.py
================ test session starts =====================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0 -- c:\python37\python.exe
cachedir: .pytest_cache
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: xdist-1.29.0, timeout-1.3.3, repeat-0.8.0, instafail-0.4.1, forked-1.0.2, emoji-0.2.0, allure-pytest-2.6.3
collected 2 items                                                                                                                                                          

test_five.py::test_task_equality FAILED                                                                                                                              [ 50%]
test_five.py::test_dict_equal FAILED                                                                                                                                 [100%]

=================== FAILURES ==============================
______________ test_task_equality _________________________
    @pytest.mark.smoke
    def test_task_equality():
        t1 = Task(‘sit there‘, ‘brain‘)
        t2 = Task(‘do something‘, ‘okken‘)
        print(t1)
        print(t2)
>       assert t1 == t2
E       AssertionError: assert Task(summary=...alse, id=None) == Task(summary=‘...alse, id=None)
E         At index 0 diff: ‘sit there‘ != ‘do something‘
E         Full diff:
E         - Task(summary=‘sit there‘, owner=‘brain‘, done=False, id=None)
E         ?                ^^^  ^^^          ^^^^
E         + Task(summary=‘do something‘, owner=‘okken‘, done=False, id=None)
E         ?               +++ ^^^  ^^^          ^^^^

test_five.py:14: AssertionError
----------------------- Captured stdout call ----------------------------------
Task(summary=‘sit there‘, owner=‘brain‘, done=False, id=None)
Task(summary=‘do something‘, owner=‘okken‘, done=False, id=None)
___________________ test_dict_equal ___________________
    @pytest.mark.dict
    @pytest.mark.smoke
    def test_dict_equal():
        t1_dict = Task(‘make sandwich‘, ‘okken‘)._asdict()
        t2_dict = Task(‘make sandwich‘, ‘okkem‘)._asdict()
        print(t1_dict)
        print(t2_dict)
>       assert t1_dict == t2_dict
E       AssertionError: assert OrderedDict([...(‘id‘, None)]) == OrderedDict([(...(‘id‘, None)])
E         Omitting 3 identical items, use -vv to show
E         Differing items:
E         {‘owner‘: ‘okken‘} != {‘owner‘: ‘okkem‘}
E         Full diff:
E         OrderedDict([(‘summary‘, ‘make sandwich‘),
E         -              (‘owner‘, ‘okken‘),
E         ?                             ^...
E
E         ...Full output truncated (5 lines hidden), use ‘-vv‘ to show

test_five.py:24: AssertionError
------------------------ Captured stdout call -------------------------------
OrderedDict([(‘summary‘, ‘make sandwich‘), (‘owner‘, ‘okken‘), (‘done‘, False), (‘id‘, None)])
OrderedDict([(‘summary‘, ‘make sandwich‘), (‘owner‘, ‘okkem‘), (‘done‘, False), (‘id‘, None)])
================ warnings summary =========================
c:\python37\lib\site-packages\_pytest\mark\structures.py:324
  c:\python37\lib\site-packages\_pytest\mark\structures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.smoke - is this a typo?  You can register custom marks to avoi
d this warning - for details, see https://docs.pytest.org/en/latest/mark.html
    PytestUnknownMarkWarning,

c:\python37\lib\site-packages\_pytest\mark\structures.py:324
  c:\python37\lib\site-packages\_pytest\mark\structures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.dict - is this a typo?  You can register custom marks to avoid
 this warning - for details, see https://docs.pytest.org/en/latest/mark.html
    PytestUnknownMarkWarning,

-- Docs: https://docs.pytest.org/en/latest/warnings.html
============== 2 failed, 2 warnings in 0.22 seconds =======================

在命令行执行使用了 -m marker_name参数,pytest在执行时会自动寻找被标记为marker_name的测试方法去执行,同时-m还支持and、or、not关键字,如下方式:

E:\Programs\Python\Python_Pytest\TestScripts>pytest -v -m "smoke and not dict" test_five.py
=========== test session starts ==============================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0 -- c:\python37\python.exe
cachedir: .pytest_cache
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: xdist-1.29.0, timeout-1.3.3, repeat-0.8.0, instafail-0.4.1, forked-1.0.2, emoji-0.2.0, allure-pytest-2.6.3
collected 2 items / 1 deselected / 1 selected                                                                                                                              

test_five.py::test_task_equality FAILED                                                                                                                              [100%]

=============== FAILURES ==================
_______________ test_task_equality ____________________________

    @pytest.mark.smoke
    def test_task_equality():
        t1 = Task(‘sit there‘, ‘brain‘)
        t2 = Task(‘do something‘, ‘okken‘)
        print(t1)
        print(t2)
>       assert t1 == t2
E       AssertionError: assert Task(summary=...alse, id=None) == Task(summary=‘...alse, id=None)
E         At index 0 diff: ‘sit there‘ != ‘do something‘
E         Full diff:
E         - Task(summary=‘sit there‘, owner=‘brain‘, done=False, id=None)
E         ?                ^^^  ^^^          ^^^^
E         + Task(summary=‘do something‘, owner=‘okken‘, done=False, id=None)
E         ?               +++ ^^^  ^^^          ^^^^

test_five.py:14: AssertionError
--------------- Captured stdout call ------------------------------
Task(summary=‘sit there‘, owner=‘brain‘, done=False, id=None)
Task(summary=‘do something‘, owner=‘okken‘, done=False, id=None)
================ warnings summary ====================
c:\python37\lib\site-packages\_pytest\mark\structures.py:324
  c:\python37\lib\site-packages\_pytest\mark\structures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.smoke - is this a typo?  You can register custom marks to avoi
d this warning - for details, see https://docs.pytest.org/en/latest/mark.html
    PytestUnknownMarkWarning,

c:\python37\lib\site-packages\_pytest\mark\structures.py:324
  c:\python37\lib\site-packages\_pytest\mark\structures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.dict - is this a typo?  You can register custom marks to avoid
 this warning - for details, see https://docs.pytest.org/en/latest/mark.html
    PytestUnknownMarkWarning,

-- Docs: https://docs.pytest.org/en/latest/warnings.html
============= 1 failed, 1 deselected, 2 warnings in 0.14 seconds ===========

跳过测试

from collections import namedtuple
import pytest
Task = namedtuple(‘Task‘, [‘summary‘,‘owner‘,‘done‘,‘id‘])
# __new__.__defaults__创建默认的Task对象
Task.__new__.__defaults__ = (None, None, False, None)

@pytest.mark.smoke
def test_task_equality():
    t1 = Task(‘sit there‘, ‘brain‘)
    t2 = Task(‘do something‘, ‘okken‘)
    print(t1)
    print(t2)
    assert t1 == t2

@pytest.mark.dict
@pytest.mark.smoke
@pytest.mark.skip(reason="跳过原因你不知道吗?")
def test_dict_equal():
    t1_dict = Task(‘make sandwich‘, ‘okken‘)._asdict()
    t2_dict = Task(‘make sandwich‘, ‘okkem‘)._asdict()
    print(t1_dict)
    print(t2_dict)
    assert t1_dict == t2_dict

执行结果:

E:\Programs\Python\Python_Pytest\TestScripts>pytest -v test_five.py
=============== test session starts ===================================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0 -- c:\python37\python.exe
cachedir: .pytest_cache
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: xdist-1.29.0, timeout-1.3.3, repeat-0.8.0, instafail-0.4.1, forked-1.0.2, emoji-0.2.0, allure-pytest-2.6.3
collected 2 items                                                                                                                                                          

test_five.py::test_task_equality FAILED                                                                                                                              [ 50%]
test_five.py::test_dict_equal SKIPPED                                                                                                                                [100%]

============== FAILURES ==================================
__________________ test_task_equality _______________________________

    @pytest.mark.smoke
    def test_task_equality():
        t1 = Task(‘sit there‘, ‘brain‘)
        t2 = Task(‘do something‘, ‘okken‘)
        print(t1)
        print(t2)
>       assert t1 == t2
E       AssertionError: assert Task(summary=...alse, id=None) == Task(summary=‘...alse, id=None)
E         At index 0 diff: ‘sit there‘ != ‘do something‘
E         Full diff:
E         - Task(summary=‘sit there‘, owner=‘brain‘, done=False, id=None)
E         ?                ^^^  ^^^          ^^^^
E         + Task(summary=‘do something‘, owner=‘okken‘, done=False, id=None)
E         ?               +++ ^^^  ^^^          ^^^^

test_five.py:14: AssertionError
------------------ Captured stdout call --------------------------------
Task(summary=‘sit there‘, owner=‘brain‘, done=False, id=None)
Task(summary=‘do something‘, owner=‘okken‘, done=False, id=None)
=========== warnings summary ==============================
c:\python37\lib\site-packages\_pytest\mark\structures.py:324
  c:\python37\lib\site-packages\_pytest\mark\structures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.smoke - is this a typo?  You can register custom marks to avoi
d this warning - for details, see https://docs.pytest.org/en/latest/mark.html
    PytestUnknownMarkWarning,

c:\python37\lib\site-packages\_pytest\mark\structures.py:324
  c:\python37\lib\site-packages\_pytest\mark\structures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.dict - is this a typo?  You can register custom marks to avoid
 this warning - for details, see https://docs.pytest.org/en/latest/mark.html
    PytestUnknownMarkWarning,

-- Docs: https://docs.pytest.org/en/latest/warnings.html
=========== 1 failed, 1 skipped, 2 warnings in 0.21 seconds ================

从测试结果中我们看到,测试用例test_five.py::test_dict_equal SKIPPED 被跳过了,然而跳过原因并没有如期的展示出来,可以使用参数-rs来展示跳过原因,如下方式:

E:\Programs\Python\Python_Pytest\TestScripts>pytest -rs test_five.py
===================== test session starts ===========================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0
rootdir: E:\Programs\Python\Python_Pytest\TestScripts
plugins: xdist-1.29.0, timeout-1.3.3, repeat-0.8.0, instafail-0.4.1, forked-1.0.2, emoji-0.2.0, allure-pytest-2.6.3
collected 2 items                                                                                                                                                          

test_five.py Fs                                                                                                                                                      [100%]

============ FAILURES ====================================
__________________________ test_task_equality _______________________
    @pytest.mark.smoke
    def test_task_equality():
        t1 = Task(‘sit there‘, ‘brain‘)
        t2 = Task(‘do something‘, ‘okken‘)
        print(t1)
        print(t2)
>       assert t1 == t2
E       AssertionError: assert Task(summary=...alse, id=None) == Task(summary=‘...alse, id=None)
E         At index 0 diff: ‘sit there‘ != ‘do something‘
E         Use -v to get the full diff

test_five.py:14: AssertionError
-------------------- Captured stdout call ----------------------------------
Task(summary=‘sit there‘, owner=‘brain‘, done=False, id=None)
Task(summary=‘do something‘, owner=‘okken‘, done=False, id=None)
========== warnings summary ====================
c:\python37\lib\site-packages\_pytest\mark\structures.py:324
  c:\python37\lib\site-packages\_pytest\mark\structures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.smoke - is this a typo?  You can register custom marks to avoi
d this warning - for details, see https://docs.pytest.org/en/latest/mark.html
    PytestUnknownMarkWarning,

c:\python37\lib\site-packages\_pytest\mark\structures.py:324
  c:\python37\lib\site-packages\_pytest\mark\structures.py:324: PytestUnknownMarkWarning: Unknown pytest.mark.dict - is this a typo?  You can register custom marks to avoid
 this warning - for details, see https://docs.pytest.org/en/latest/mark.html
    PytestUnknownMarkWarning,

-- Docs: https://docs.pytest.org/en/latest/warnings.html
============ short test summary info ====================
SKIPPED [1] test_five.py:17: 跳过原因你不知道吗?
========== 1 failed, 1 skipped, 2 warnings in 0.14 seconds =================

还可以给跳过添加理由,例如当selenium版本是1.0.4的时候跳过

from collections import namedtuple
import pytest
import selenium
Task = namedtuple(‘Task‘, [‘summary‘,‘owner‘,‘done‘,‘id‘])
# __new__.__defaults__创建默认的Task对象
Task.__new__.__defaults__ = (None, None, False, None)

@pytest.mark.smoke
def test_task_equality():
    t1 = Task(‘sit there‘, ‘brain‘)
    t2 = Task(‘do something‘, ‘okken‘)
    print(t1)
    print(t2)
    assert t1 == t2

@pytest.mark.dict
@pytest.mark.smoke
@pytest.mark.skipif(selenium.__version__ == ‘1.0.4‘, reason="跳过原因你不知道吗?")
def test_dict_equal():
    t1_dict = Task(‘make sandwich‘, ‘okken‘)._asdict()
    t2_dict = Task(‘make sandwich‘, ‘okkem‘)._asdict()
    print(t1_dict)
    print(t2_dict)
    assert t1_dict == t2_dict

运行测试子集

运行单个目录,只需要将目录作为pytest的参数即可

pytest -v test/func  --tb=no
1
运行单个测试函数,只需要在文件名后添加::符号和函数名

pytest -v test/func/test_five.py::test_dict_equal
1
运行单个测试类,与运行单个测试函数类似,在文件名后添加::符号和类名

pytest -v test/func/test_five.py::Test_Update
1
运行单个测试类中的测试方法,在文件名后添加::符号和类名然后再跟::符号和函数名

pytest -v test/func/test_five.py::Test_Update::test_dict_equal










原文地址:https://www.cnblogs.com/davieyang/p/11684621.html

时间: 2024-07-30 19:19:02

Pytest编写测试函数的相关文章

用 pytest 测试 python 代码

Pytest 是一个比较成熟且功能完备的 Python 测试框架.其提供完善的在线文档,并有着大量的第三方插件和内置帮助,适用于许多小型或大型项目.Pytest 灵活易学,打印调试和测试执行期间可以捕获标准输出,适合简单的单元测试到复杂的功能测试.还可以执行 nose, unittest 和 doctest 风格的测试用例,甚至 Django 和 trial.支持良好的集成实践, 支持扩展的 xUnit 风格 setup,支持非 python 测试.支持生成测试覆盖率报告,支持 PEP8 兼容的

如何编写单元测试

如何编写单元测试 燕双龙 一 单元测试简介 单元测试是代码正确性验证的最重要的工具,也是系统测试当中最重要的环节.也是唯一需要编写代码才能进行测试的一种测试方法.在标准的开发过程中,单元测试的代码与实际程序的代码具有同等的重要性.每一个单元测试,都是用来定向测试其所对应的一个单元的数据是否正确. 单元测试是由程序员自己来完成,最终受益的也是程序员自己.可以这么说,程序员有责任编写功能代码,同时也就有责任为自己的代码编写单元测试.执行单元测试,就是为了证明这段代码的行为和我们期望的一致. 单元测试

unittest和pytest的区别

一.用例编写规则 1.unittest提供了test cases.test suites.test fixtures.test runner相关的类,让测试更加明确.方便.可控.使用unittest编写用例,必须遵守以下规则: (1)测试文件必须先import unittest (2)测试类必须继承unittest.TestCase (3)测试方法必须以"test_"开头 (4)测试类必须要有unittest.main()方法 2.pytest是python的第三方测试框架,是基于un

软件测试知识点汇总目录(持续更新)

个人在工作之余通过word文档长期持续更新工作中需要涉及到的一些理论和技术知识.所谓好记记性,不如乱笔头.根据工作年限和职位的变化,以及就职公司参与的产品或者项目所涉及到的测试方面的技能不一样,会存在有些之前的技能不经常使用,会导致生疏的现象.虽然不至于归零,但是一旦需要使用的时候,有一个相对比较完整规范的文档来应急阅读来回顾其使用等是很有帮助的.比在网上搜索出来的相关零散的不完整的知识点方便的多. 文档创建年限不是很长,有很多知识项没有写入文档或者还没有来得及编写,需要在后续持续更新.文档编写

决策树ID3算法预测隐形眼睛类型--python实现

本节讲解如何预测患者需要佩戴的隐形眼镜类型. 1.使用决策树预测隐形眼镜类型的一般流程 (1)收集数据:提供的文本文件(数据来源于UCI数据库) (2)准备数据:解析tab键分隔的数据行 (3)分析数据:快速检查数据,确保正确地解析数据内容,使用createPlot()函数绘制最终的树形图 (4)训练算法:createTree()函数 (5)测试算法:编写测试函数验证决策树可以正确分类给定的数据实例 (6)使用算法:存储数的数据结构,以使下次使用时无需重新构造树 trees.py如下: #!/u

前端之 —— node.js摸爬打滚之路(二)

这篇主要学习: 测试框架mocha; 断言库:should; 测试率覆盖工具 istanbul; 创建并进入lesson3: mkdir lesson3 && lesson3 创建main.js并编写测试函数: var fibonacci = function(n) { if(typeof n !== 'number'){ throw new Error('n should be a Number') } if(n < 0){ throw new Error('n should &g

AndroidNative层文件解析漏洞挖掘指南

| 导语 本文以手Q的一次文件解析类漏洞挖掘为例,叙述了Android Native层文件解析类型漏洞挖掘的过程 手Q这个应用从功能来说十分的庞大,如果使用类似MFFA的框架去挖掘文件解析类漏洞,效率低,而且文件入口在哪儿.如何利用脚本进行自动化都是非常大的问题.本文在一次手Q的文件解析类漏洞挖掘的过程中,提出了一种可能的解决问题的方案,妄称指南不吝赐教. 目录: 1.问题分析 2.流程图 3.so筛选 4.测试程序编写 5.test case生成 6.测试得出crash 7.未来的工作 0x0

嵌入式Linux裸机开发(七)——UART串口通信

嵌入式Linux裸机开发(七)--UART串口通信 一.UART串口通信简介 通用异步收发器简称UART,即UNIVERSAL ASYNCHRONOUS RECEIVER AND TRANSMITTER, 它用来传输串行数据.发送数据时, CPU 将并行数据写入UART,UAR按照一定的格式在一根电线上串 行发出:接收数据时, UART检测另一根电线的信号,将串行收集在缓冲区中, CPU 即可读取 UART 获得这些数据. 在 S5PV210中, UART提供了 4 对独立的异步串口I/O端口,

python核心编程--笔记

python核心编程--笔记 的解释器options: 1.1 –d   提供调试输出 1.2 –O   生成优化的字节码(生成.pyo文件) 1.3 –S   不导入site模块以在启动时查找python路径 1.4 –v   冗余输出(导入语句详细追踪) 1.5 –m mod 将一个模块以脚本形式运行 1.6 –Q opt 除法选项(参阅文档) 1.7 –c cmd 运行以命令行字符串心事提交的python脚本 1.8 file   以给定的文件运行python脚本 2 _在解释器中表示最后