【问题标题】:how to assert a binary multiline value in python using pytest如何使用pytest在python中断言二进制多行值
【发布时间】:2021-12-11 04:54:03
【问题描述】:

我有以下使用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.''
)

它似乎不起作用

任何帮助都会有所帮助,提前致谢。

【问题讨论】:

  • 断言整个内容始终相同?
  • "基于它是多行的事实" 这似乎并非如此。 file_pointer.get_value() 中没有换行符。不过,您的参考字符串缺少空格。

标签: python python-3.x pytest stringio


【解决方案1】:

使用三引号。最初的单行作业在 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.""")

【讨论】:

  • 简单的"-quotes 也应该足够了。
  • 没错,但这可能会与其他参考字符串中断。我相信,长格式正文的首选/pythonic 方法是三引号。
猜你喜欢
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
相关资源
最近更新 更多