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文件都进行测试。

  • 相关阅读:
    Vuex
    浏览器渲染页过程描述
    mvvm 模式
    flex 布局
    js 浮点数计算
    3、异步编程-JS种事件队列的优先级
    高阶函数 debounce 和 throttle
    记录学习MVC过程,HTML铺助类(二)
    记录学习MVC过程,控制器方法和视图(一)
    修改以前项目遇到,所有页面继承BaseBage,Sesssion保存一个model,实现登录(记录一下)
  • 原文地址:https://www.cnblogs.com/aomi/p/7213907.html
Copyright © 2011-2022 走看看