pytest (4) assert assertion

Pytest When writing test, the test fails to transmit information, you can directly use the built-in Python assert keyword
Pytest allows you to add any expression after the assert keyword, if bool conversion equal to False, it means failure, pytest there is an important function can be rewritten assert keyword, pytest can truncate assert assert to replace pytest provided,

For example as follows:

from collections import namedtuple
import pytest
import time

#clooections  是Python内建的一个集合模块,提供了许多有用的集合类
Task = namedtuple('task',['summary','owner','done','id'])
# 使用__new__.default__创建默认的Task对象,不需要指定所有的属性
Task.__new__.__defaults__ = (None,None,False,None)

def test_asdict():

  t_task =  Task('do somehing','okken',True,21)
  print(type(t_task),t_task)
  #返回一个将字段类型映射到其值的新dict
  t_dict =  t_task._asdict()
  expected = {'summary':'do somehing',
              'owner':'okken',
              'done':True,
              'id':21}
  assert t_dict == expected

@pytest.mark.run_these_please
def test_replace():
    time.sleep(2)
    t_before = Task('finish book','brian',False)
    print(t_before)
    # __replace 返回一个新的命名元祖对象,用新值替换指定的字段
    t_after = t_before._replace(id =10,done = True)
    t_expected = Task('finish book','brian',True,11)
    assert t_after == t_expected

Use the original assert


7565187-93b80fbc7c166eea.png
image.png

Use assert pytest of


7565187-7694e31991d3a95d.png
image.png

Reproduced in: https: //www.jianshu.com/p/4ff89555c3fa

Guess you like

Origin blog.csdn.net/weixin_34161032/article/details/91075361