通俗易懂使用pytest测试代码

pytest 快速开发和使用

1. 什么是pytest?

pytest 强大的python测试工具,它可以用所有类型和级别的软件测试。pytest可以被开发团队,QA团队,独立测试小组,实践TDD的个人和开源代码项目。pytest是软件测试框架,这意味着pytest是命令行工具,它会自动找到你写的测试,运行测试并报告结果。可编写插件或安装第三方来扩展插件。

pytest脱颖而出的原因:
1 简单
2 易读
3 用assert来测试失败
4 可运行unittest或nose测试

2 如何运行并测试

2.1 安装pytest

pytest安装比较简单,pip install -U pytest 即可
记住要添加 -U 选项

2.2 测试用例

def test_passing():
assert (1, 2, 3) == (1, 2, 3)

2.2.1 python解析exe 程序获得输出
import os


path = "/home/suda/samples/0_Simple/vectorAdd/vectorAdd"

a = os.system(path)

print(a, type(a))

def test_exe():
    assert a == 0

执行指令并输出结果:

pytest -v --junitxml=report.xml
platform linux -- Python 3.6.9, pytest-6.2.5, py-1.9.0, pluggy-0.13.1 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /home/suda/cplus_test/pytest_case
collected 6 items

pass_test.py::test_passing PASSED                                                                                       [ 16%]
task1_test.py::test_defaults PASSED                                                                                     [ 33%]
task1_test.py::test_member_access PASSED                                                                                [ 50%]
task2_test.py::test_asdict PASSED                                                                                       [ 66%]
task2_test.py::test_replace PASSED                                                                                      [ 83%]
test_exe.py::test_exe PASSED                                                                                            [100%]

------------------------------ generated xml file: /home/suda/cplus_test/pytest_case/report.xml -------------------------------

pytest -v --junitxml=report.xml 运行结果保存到report.xml

####2.2.2 python测试整个tensorflow code

#coding:utf8
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession

config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)

import os
#os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"


def main():

    #导入数据,创建一个session对象 ,之后的运算都会跑在这个session里
    mnist = input_data.read_data_sets("MNIST/",one_hot=True)
    sess = tf.Session()

    #加载模型
    saver = tf.train.import_meta_graph('./models/mnist.meta')
    saver.restore(sess, tf.train.latest_checkpoint('./models'))


    graph = tf.get_default_graph()

    x_ = graph.get_tensor_by_name("x:0")

    # Now, access the op that you want to run.
    y_ = graph.get_tensor_by_name("y_conv:0")
    #y=tf.placeholder(tf.float32,[None,10])  #训练数据的标签,10分类

    for i in range(20):
        batch = mnist.test.next_batch(1)
        #预测
        #print(batch.type)
        y_conv =  sess.run(y_,feed_dict={x_: batch[0]})
        correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(batch[1], 1))
        print(correct_prediction.eval(session=sess))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))

        print("test accuracy: ",accuracy.eval(feed_dict={x_: mnist.test.images, y_: mnist.test.labels}))

    return 0

#print(main())

def test_main():
    assert 0 == main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值