Python Unit Test Framework--pytest

Source: Internet
Author: User
Tags python script

Official website: https://docs.pytest.org/en/latest/

Pytest to help you write a better program.

1. Installation:

Ubuntu16.04 Installing Pytest:

PIP3 Install Pytest

Pytest--version

This is Pytest version 3.2.3, imported from/usr/local/lib/python3.5/dist-packages/pytest.py

2, an example of a easy test: (a simple example), named test_pytest1.py

1 def funx (x): 2     return x + 1345def  test_answer ():6      Assert funx (2) = = 5

Run:

Go to Python script path: pytest test_pytest1.py

[Email protected]:/home/ranxf/python3 Unit Test/demo# pytest test_pytest1.py
============================= test Session starts ==============================
Platform Linux--Python 3.5.2, pytest-3.2.3, py-1.4.34, pluggy-0.4.0
RootDir:/home/ranxf/python3 unit Test/demo, Inifile:
Collected 1 Item

test_pytest1.py F

=================================== Failures ===================================
_________________________________ Test_answer __________________________________

Def test_answer ():
> Assert Funx (2) = = 5
E Assert 3 = = 5
E + where 3 = Funx (2)

Test_pytest1.py:8: Assertionerror
=========================== 1 failed in 0.02 seconds ===========================

Enter the Python script path: Pytest-q test_pytest1.py (plus a parameter-Q) to run the result:

[Email protected]:/home/ranxf/python3 Unit Test/demo# pytest-q test_pytest1.py
F
=================================== Failures ===================================
_________________________________ Test_answer __________________________________

Def test_answer ():
> Assert Funx (2) = = 5
E Assert 3 = = 5
E + where 3 = Funx (2)

Test_pytest1.py:8: Assertionerror
1 failed in 0.02 seconds

There is a little difference between the two running results, which is that some version information is missing.

3. Create multiple test cases in a test class:

1 #a test class to create multiple test cases2 3 4 classTestClass:5     defTest_one (self):6x =" This"7         assert "s" inchx8 9     defTest_two (self):Tenx ="Hello" One         assertx = ="Hi"
View Code

Python Unit Test Framework--pytest

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.