Pytest testing framework tutorial (a)

First, the quick start Pytest testing framework

--------- This article is reproduced in ethanol ( http://www.testclass.net/pytest/quick_start )

Brief introduction

pytest testing framework allows us to easily write test cases, these use cases to write simple, but still can scale and write more complex test cases.

Official Documents

installation

pip install -U pytest

Use the following command to check whether the successful installation pytest

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

Quick Start

Create a named test_quick_start.pyfile, knocking the following

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

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

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

The above code does two things

  • This defines a reverse(string)global function, the role is reversed and returns the string. For example, enter "abc" will reverse into "cba"
  • This defines a test_reverse()function, it contains two assertions to test the reverse()method of the correctness

Using the following command line to run with the embodiment

pytest

The result should be as follows

========================================================================= 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 =======================================================================

to sum up

This is the most simple unit test cases, to achieve the object of the test code with the code.

Guess you like

Origin www.cnblogs.com/x1you/p/11233435.html