Pytest框架教程(一)

本文介绍如何使用pytest测试框架编写简单的单元测试用例。通过定义一个字符串反转函数和对应的测试函数,演示pytest的基本用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、快速上手Pytest框架

此文章转载于---------乙醇 (http://www.testclass.net/pytest/quick_start

简介

pytest测试框架可以让我们很方便的编写测试用例,这些用例写起来虽然简单,但仍然可以规模化以及编写更加复杂的测试用例。

官方文档

安装

pip install -U pytest

用下面的命令去检查一下pytest是否成功安装

$ pytest --version
This is pytest version 3.x.y, imported from $PYTHON_PREFIX/lib/python3.5/site-packages/pytest.py

快速开始

创建名为test_quick_start.py的文件,敲如下内容

def reverse(string):
    return string[::-1]

def test_reverse():
    string = "good"
    assert reverse(string) == "doog"

    another_string = "itest"
    assert reverse(another_string) == "tseti"

上面的代码做了2件事情

  • 定义了名为reverse(string)的全局函数,作用是把string反转并返回。比如输入"abc"会反转成"cba"
  • 定义了名为test_reverse()的函数,包含了2个断言,用来测试reverse()方法的正确性

在命令行中使用下面的命令去运行用例

pytest

结果应该大致如下

========================================================================= test session starts =========================================================================
platform darwin -- Python 2.7.12, pytest-3.2.3, py-1.4.34, pluggy-0.4.0
rootdir: /Users/easonhan/code/testclass.net/src/pytest, inifile:
collected 1 item

test_quick_start.py .

====================================================================== 1 passed in 0.01 seconds =======================================================================

总结

这是最简单的单元测试实例,实现了用代码去测试代码的目的。

转载于:https://www.cnblogs.com/x1you/p/11233435.html

pytest Quick Start Guide pdf 来源:[pytest Quick Start Guide - 2018.pdf](https://itbooks.ctfile.com/fs/18113597-314070249) Learn the pytest way to write simple tests which can also be used to write complex tests Key Features Become proficient with pytest from day one by solving real-world testing problems Use pytest to write tests more efficiently Scale from simple to complex and functional testing Book Description Python's standard unittest module is based on the xUnit family of frameworks, which has its origins in Smalltalk and Java, and tends to be verbose to use and not easily extensible.The pytest framework on the other hand is very simple to get started, but powerful enough to cover complex testing integration scenarios, being considered by many the true Pythonic approach to testing in Python. In this book, you will learn how to get started right away and get the most out of pytest in your daily work?ow, exploring powerful mechanisms and plugins to facilitate many common testing tasks. You will also see how to use pytest in existing unittest-based test suites and will learn some tricks to make the jump to a pytest-style test suite quickly and easily. What you will learn Write and run simple and complex tests Organize tests in fles and directories Find out how to be more productive on the command line Markers and how to skip, xfail and parametrize tests Explore fxtures and techniques to use them effectively, such as tmpdir, pytestconfg, and monkeypatch Convert unittest suites to pytest using little-known techniques Use third-party plugins Who this book is for This book is for Python programmers that want to learn more about testing. This book is also for QA testers, and those who already benefit from programming with tests daily but want to improve their existing testing tools.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值