简体   繁体   English

使用 pytest 断言 boolean 值的推荐方法 - 相关的 pylint 警告

[英]Recommented way to assert boolean value with pytest - related pylint warning

I would like to assert that an expression equals some boolean value:我想断言表达式等于某个 boolean 值:

assert result['foo'][0] == False
assert result['foo'][1] == True

However, pylint suggests to use is or 'not' instead of == :但是,pylint 建议使用is or 'not' 而不是==

Comparison 'result['foo'][0] == True' should be 'result['foo'][0] is True' if checking for the singleton value False, or 'not result['a'][0]' if testing for falsiness (singleton-comparison)如果检查 singleton 值为 False,比较 'result['foo'][0] == True' 应该是 'result['foo'][0] is True',或者 'not result['a'][0] ' 如果测试虚假性(单例比较)

If I use 'is' the test fails.如果我使用“是”,则测试失败。

If I use 'not' I find the expression harder to read/interpret because it seems to be less explicit:如果我使用“不”,我会发现该表达式更难阅读/解释,因为它似乎不太明确:

assert not result['foo'][0]
assert result['foo'][1]

a) Is the last way really the best practice to assert boolean expressions and I should get used to it? a)最后一种方法真的是断言 boolean 表达式的最佳实践吗?我应该习惯它吗?

b) Or should I disable the warning? b)或者我应该禁用警告?

c) Or should I use something like c)或者我应该使用类似的东西

assertIsFalse(result['foo'][0])
assertIsTrue(result['foo'][1])

or或者

assert falsy(result['foo'][0])
assert truthy(result['foo'][1])

Further notes:进一步说明:

  1. The pytest documentation does not seem to have a recommendation on how to assert boolean values: pytest 文档似乎没有关于如何断言 boolean 值的建议:

https://docs.pytest.org/en/7.1.x/how-to/assert.html https://docs.pytest.org/en/7.1.x/how-to/assert.html

  1. Pytest does not seem to provide extra assertion methods like assertIsTrue or truthy : Pytest似乎没有提供额外的断言方法,如assertIsTruetruthy

https://docs.pytest.org/en/4.6.x/reference.html#functions https://docs.pytest.org/en/4.6.x/reference.html#functions

  1. Unittest provides methods assertTrue , assertFalse . Unittest提供方法assertTrue , assertFalse However, that would require to derive from the unittest class.但是,这需要从单元测试 class 派生。

https://www.pythontutorial.net/python-unit-testing/python-asserttrue/ https://www.pythontutorial.net/python-unit-testing/python-asserttrue/

  1. Numpy does not seem to include assertion methods for boolean values: Numpy似乎不包括 boolean 值的断言方法:

https://numpy.org/devdocs/reference/routines.testing.html#asserts https://numpy.org/devdocs/reference/routines.testing.html#asserts

I wrote some custom helper functions to improve readability, based on the built in bool() function:我基于内置的 bool() function 编写了一些自定义帮助函数来提高可读性:

def truthy(value):
    return bool(value)


def falsy(value):
    return not bool(value)

usage:用法:

assert falsy(result['foo'][0])
assert truthy(result['foo'][1])

https://www.geeksforgeeks.org/truthy-vs-falsy-values-in-python/ https://www.geeksforgeeks.org/truthy-vs-falsy-values-in-python/

pytest is using assertion directly so you don't have to learn about function names and existence and directly use what the language offer and display error nicely. pytest 直接使用断言,因此您不必了解 function 的名称和存在,直接使用语言提供的内容并很好地显示错误。 So, if you want to test for falsyness/trutheyness with pytest you do this::所以,如果你想用 pytest 测试虚假/真实性,你可以这样做::

assert not result['foo'][0]
assert result['foo'][1]

If you want to check that something is literally False or True then you do this::如果你想检查某些东西实际上是False还是True那么你可以这样做::

assert result['foo'][0] is False
assert result['foo'][1] is True

Creating a function that does something that already exists in python because you're not familiar with it, is not a good practice.创建一个 function 来执行 python 中已经存在的事情,因为您不熟悉它,这不是一个好的做法。 Use the tools of the language everyone familiar with python is already using.使用每个熟悉 python 的人都在使用的语言工具。 Otherwise reading your codebase will have a cost of entry for things that you invented and that no one is familiar with.否则,阅读您的代码库将需要为您发明的、无人熟悉的事物付出代价。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM