zoukankan      html  css  js  c++  java
  • pytest学习(1)

    pytest是python的一个测试框架,主要是用来进行一些小的测试。

    在pycharm中,缺省用的是unittest,这里说明如何设置为pytest。

    当然,第一部是安装pytest

    pip3 install pytest

    然后,在pycharm中,files-》settings-》tools=》python integrated tools=》设定default test runner

    然后,写一个简单的py程序(记住,test_*.py or *_test.py):

    def inc(x):
        return x + 1
    
    def test_answer():
        assert inc(3) == 5
    
    def test_wcf():
        assert inc(3) > 5
    
    def test_hy():
        assert inc(3) < 5
    

      运行后:

    C:Python35python.exe C:UserswcfAppDataLocalJetBrainsToolboxappsPyCharm-Pch-0171.4694.67helperspycharm\_jb_pytest_runner.py --path C:/fitme/work/nltk/test_5.py
    Testing started at 18:53 ...
    Launching py.test with arguments C:/fitme/work/nltk/test_5.py in C:fitmework
    ltk
    ============================= test session starts =============================
    platform win32 -- Python 3.5.3, pytest-3.1.3, py-1.4.34, pluggy-0.4.0
    rootdir: C:fitmework
    ltk, inifile:
    collected 3 items
    
    test_5.py F
    test_5.py:3 (test_answer)
    def test_answer():
    >       assert inc(3) == 5
    E       assert 4 == 5
    E        +  where 4 = inc(3)
    
    test_5.py:5: AssertionError
    F
    test_5.py:6 (test_wcf)
    def test_wcf():
    >       assert inc(3) > 5
    E       assert 4 > 5
    E        +  where 4 = inc(3)
    
    test_5.py:8: AssertionError
    .
    
    ================================== FAILURES ===================================
    _________________________________ test_answer _________________________________
    
        def test_answer():
    >       assert inc(3) == 5
    E       assert 4 == 5
    E        +  where 4 = inc(3)
    
    test_5.py:5: AssertionError
    __________________________________ test_wcf ___________________________________
    
        def test_wcf():
    >       assert inc(3) > 5
    E       assert 4 > 5
    E        +  where 4 = inc(3)
    
    test_5.py:8: AssertionError
    ===================== 2 failed, 1 passed in 0.05 seconds ======================
    
    Process finished with exit code 0
    

     如果只运行pytest,则对当前目录下所有的test_*.py or *_test.py文件都进行测试。

  • 相关阅读:
    [Usaco2005 open]Expedition
    舞会
    双栈维护之--Hdu4699 editor
    利用两个堆来维护第K大之Poj3784 Running Median
    Zju1061Web Navigation 网络导航
    Qsort求静态的第K大
    BZOJ2726【SDOI2012】任务安排(斜率优化Dp+二分查找)
    P2365 任务安排 斜率优化入门
    任务处理--斜率优化Dp入门
    结构体排序教学
  • 原文地址:https://www.cnblogs.com/aomi/p/7213907.html
Copyright © 2011-2022 走看看