美文网首页
Chapter 1 Installation and Getti

Chapter 1 Installation and Getti

作者: U一like | 来源:发表于2018-06-13 22:33 被阅读4次

    安装 pytest

    1. 在命令行中运行以下命令:
      pip install -U pytest
    2. 检查当前安装的版本:
      pytest --version

    创建你的第一个测试

    创建一个简单的只有四行的测试方法

    # content of test_sample.py
    def func(x):
      return x + 1
    
    def test_answer():
      assert func(3) == 5
    

    就是这样. 现在你可以执行这个测试方法了.

    $ pytest
    =========================== test session starts ============================
    platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y
    rootdir: $REGENDOC_TMPDIR, inifile:
    collected 1 item
    test_sample.py F [100%]
    ================================= FAILURES =================================
    _______________________________ test_answer ________________________________
    def test_answer():
    > assert func(3) == 5
    E assert 4 == 5
    E + where 4 = func(3)
    test_sample.py:5: AssertionError
    ========================= 1 failed in 0.12 seconds =========================
    

    这里测试返回了一个错误报告, 因为func(3) 不能返回 5.

    Note: 你可以使用`assert`声明验证测试预期. pytest的高级断言内省将会
    

    智能地报告assert表达式的中间值,这样您就可以避免JUnit遗留的许多名称
    方法

    运行多个测试

    pytest将会运行当前文件夹及其子文件夹中所有 test_.py* 及*_test.py格式的测试. 更普遍的, 请参阅 standard test discovery rules.

    断言引发了某个异常

    断言一些代码引发异常通过使用raise这一助手

    # content of test_sysexit.py
    import pytest
    
    def f():
      raise SystemExit(1)
    
    def test_mytest():
      with pytest.raises(SystemExit):
        f()
    

    通过安静报告模式执行测试功能

    $ pytest -q test_sysexit.py
    .                                                                           [100%]
    1 passed in 0.12 seconds
    

    在一个类中运行多个测试用例

    你也许想要将一次开发的多个测试用例放到一个类里. pytest使创建一个类来控制超过一个测试用例变得很简单:

    # content of test_class.py
    class TestClass(object):
      def test_one(self):
        x = 'this'
        assert 'h' in x
    
      def test_two(self):
        x = 'hello'
        assert hasattr(x, 'check')
    

    pytest 遵循Python测试发现约定来发现所有测试用例, 因此它会找到所有test_开头的方法. 这里不需要子类化任何东西. 我们能够简单的通过传递模块文件名来运行模块:

    $ pytest -q test_class.py
    .F [100%]
    ================================= FAILURES =================================
    ____________________________ TestClass.test_two ____________________________
    self = <test_class.TestClass object at 0xdeadbeef>
    def test_two(self):
    x = "hello"
    > assert hasattr(x, 'check')
    E AssertionError: assert False
    E + where False = hasattr('hello', 'check')
    test_class.py:8: AssertionError
    1 failed, 1 passed in 0.12 seconds
    

    第一个用例是'Passed', 第二个是'Failed'. 你能够很容易的看到断言中的中间值来帮助你理解失败的原因.

    为功能测试请求一个唯一的临时目录

    # content of test_tmpdir.py
    def test_needsfiles(tmpdir):
      print(tmpdir)
      assert 0
    

    在测试函数签名中列出名称tmpdir, pytest将查找并调用一个fixture工厂来创建
    执行测试函数调用之前的资源.Pytest 创建一个唯一的每个测试调用的临时目录:

    $ pytest -q test_tmpdir.py
    F [100%]
    ================================= FAILURES =================================
    _____________________________ test_needsfiles ______________________________
    tmpdir = local('PYTEST_TMPDIR/test_needsfiles0')
    def test_needsfiles(tmpdir):
    print (tmpdir)
    > assert 0
    E assert 0
    test_tmpdir.py:3: AssertionError
    --------------------------- Captured stdout call ---------------------------
    PYTEST_TMPDIR/test_needsfiles0
    1 failed in 0.12 seconds
    

    更多关于临时文件的处理请参阅Temporary directories and files

    pytest --fixtures # 显示内置和自定义装置
    

    相关文章

      网友评论

          本文标题:Chapter 1 Installation and Getti

          本文链接:https://www.haomeiwen.com/subject/zveqeftx.html