首页 > 解决方案 > 如何使用pytest在python中断言二进制多行值

问题描述

我有以下使用场景python3

type(file_pointer)
=> <class '_io.BytesIO'>

然后


file_pointer.get_value()

# result below

b'simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.'

什么是断言上述内容的好方法,pythonpytest基于它是多行的事实

我努力了:

assert file_pointer == (
  'b'simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the' 
  'industry's standard dummy text ever since the 1500s, when an unknown printer took a' 
  'galley of type and scrambled it to make a type specimen book. It has survived not only' 
  'five centuries, but also the leap into electronic typesetting, remaining essentially' 
  'unchanged. It was popularised in the 1960s with the release of Letraset sheets'
  'containing Lorem Ipsum passages, and more recently with desktop publishing software' 
  'like Aldus PageMaker including versions of Lorem Ipsum.''
)

它似乎不起作用

任何帮助都会有所帮助,谢谢。

标签: pythonpython-3.xpyteststringio

解决方案


使用三引号。最初的单行作业在单词industry's中有撇号的地方中断。

assert file_pointer.get_value() == (
  b"""simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.""")

推荐阅读