html随机选数代码介绍 html随机数生成器( 三 )


[dechin@dechin-manjaro pytest]$ pytest --versionpytest 6.2.2pytest单元测试用例撰写根据前面一个章节中的random_number.py文件,我们可以对照的写一个简单测试用例:
# test_random_number.pyimport pytestfrom random_number import random_number_generator as rngdef test_random_number_generator():for i in range(10):random_number = rng()assert random_number == 0 or random_number == 1该测试用例的含义为:导入rng函数之后,测试10次该函数的返回值,所返回的值必须是0或者1的随机数,如果输出了这两个数字以外的返回结果,那么说明这个随机数产生器功能上存在问题 。基于pytest的测试代码可以通过如下的指令来运行:
[dechin@dechin-20n2s01200 pytest]$ py.test=========================================== test session starts ============================================platform linux -- Python 3.8.5, pytest-6.2.2, py-1.9.0, pluggy-0.13.1rootdir: /home/dechin/projects/2021-python/pytestplugins: cov-2.11.1, metadata-1.11.0, html-3.1.1collected 1 itemtest_random_number.py .[100%]============================================= warnings summary =============================================../../../anaconda3/lib/python3.8/site-packages/projectq/ops/_gates.py:118/home/dechin/anaconda3/lib/python3.8/site-packages/projectq/ops/_gates.py:118: PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal with linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code to use regular ndarray.return np.matrix([[1, 0], [0, 1j]])../../../anaconda3/lib/python3.8/site-packages/projectq/ops/_gates.py:133/home/dechin/anaconda3/lib/python3.8/site-packages/projectq/ops/_gates.py:133: PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal with linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code to use regular ndarray.return np.matrix([[1, 0], [0, cmath.exp(1j * cmath.pi / 4)]])test_random_number.py: 40 warnings/home/dechin/anaconda3/lib/python3.8/site-packages/projectq/ops/_gates.py:69: PendingDeprecationWarning: the matrix subclass is not the recommended way to represent matrices or deal with linear algebra (see https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html). Please adjust your code to use regular ndarray.return 1. / cmath.sqrt(2.) * np.matrix([[1, 1], [1, -1]])-- Docs: https://docs.pytest.org/en/stable/warnings.html====================================== 1 passed, 42 warnings in 0.50s ======================================从返回的结果来看,出现了1 passed而没有failed,说明所有的测试用例都已经执行成功了,但是这里存在不少的告警warnings信息 。
pytest初始化配置文件在上一节的测试结果中,我们发现有非常多的测试告警 。假如我们确认这些告警信息可以忽略,那么我们可以通过在指令中配置忽略告警信息,或者直接使用这里介绍的pytest.ini来忽略相应的告警信息:
# pytest.ini[pytest]filterwarnings =ignore::PendingDeprecationWarning在当前目录下的ini配置文件中,我们添加了PendingDeprecationWarning作为忽略项,然后我们再回头看一下上述用例的测试结果:
[dechin@dechin-manjaro pytest]$ py.test=========================================== test session starts ============================================platform linux -- Python 3.8.5, pytest-6.2.2, py-1.9.0, pluggy-0.13.1rootdir: /home/dechin/projects/2021-python/pytest, configfile: pytest.iniplugins: cov-2.11.1, metadata-1.11.0, html-3.1.1collected 1 itemtest_random_number.py .[100%]============================================ 1 passed in 0.50s =============================================这里返回的结果中就没有告警信息了 。
pytest生成html格式报告为了更好的展现测试的结果,这里我们需要先安装一个组件pytest-html:


以上关于本文的内容,仅作参考!温馨提示:如遇健康、疾病相关的问题,请您及时就医或请专业人士给予相关指导!

「四川龙网」www.sichuanlong.com小编还为您精选了以下内容,希望对您有所帮助: