[英]Can I run unittest / pytest with python Optimization on?
I just added a few assert
statements to the constructor of a class.我刚刚在 class 的构造函数中添加了一些
assert
语句。
This has had the immediate effect of making about 10 tests fail.这立即导致大约 10 次测试失败。
Rather than fiddle with those tests I'd just like to run pytest in Python's Optimization switched on ( -O
switch, which means the assert
s are all ignored).而不是摆弄这些测试,我只想在 Python 的优化中运行 pytest (
-O
开关,这意味着assert
s都被忽略)。 But looking at the docs and searching I can't find a way to do this.但是查看文档和搜索我找不到这样做的方法。
I'm slightly wondering whether this might be bad practice, as arguably the time to see whether assert
s fail may be during testing.我有点想知道这是否是不好的做法,可以说是在测试期间查看
assert
是否失败的时间。
On the other hand, another thought is that you might have certain tests (integration tests, etc.) which should be run without optimisation, so that the assert
s take effect, and other tests where you are being less scrupulous about the objects you are creating, where it might be justifiable to ignore the assert
s.另一方面,另一个想法是您可能有某些测试(集成测试等)应该在没有优化的情况下运行,以便
assert
生效,以及您对自己的对象不那么谨慎的其他测试创建,忽略assert
s 可能是合理的。
assert
s obviously qualify as "part of testing"... I'd like to add more to some of my constructors and other methods, typically to check parameters, but without making hundreds of tests fail, or have to become much more complicated. assert
显然有资格作为“测试的一部分”......我想在我的一些构造函数和其他方法中添加更多内容,通常用于检查参数,但不会使数百个测试失败,或者必须变得更加复杂。
The best way in this case would be to move all assert statements inside your test code.在这种情况下,最好的方法是将所有断言语句移动到测试代码中。 Maybe even switch to https://pytest.org/ as it is already using assert for test evaluation.
甚至可能切换到https://pytest.org/因为它已经在使用 assert 进行测试评估。
I'm assuming you can't in fact do this.我假设你实际上不能这样做。
Florin and chepner have both made me wonder whether and to what extent this is desirable. Florin 和 chepner 都让我想知道这是否以及在多大程度上是可取的。 But one can imagine various ways of simulating something like this, for example a
Verifier
class:但是可以想象各种模拟这样的事情的方法,例如
Verifier
者 class:
class ProjectFile():
def __init__(self, project, file_path, project_file_dict=None):
self.file_path = file_path
self.project_file_dict = project_file_dict
if __debug__:
Verifier.check(self, inspect.stack()[0][3]) # gives name of method we're in
class Verifier():
@staticmethod
def check(object, method, *args, **kwargs):
print(f'object {object} method {method}')
if type(object) == ProjectFile:
project_file = object
if method == '__init__':
# run some real-world checks, etc.:
assert project_file.file_path.is_file()
assert project_file.file_path.suffix.lower() == '.docx'
assert isinstance(project_file.file_path, pathlib.Path)
if project_file.project_file_dict != None:
assert isinstance(project_file.project_file_dict, dict)
Then you can patch out the Verifier.check
method easily enough in the testing code:然后,您可以在测试代码中轻松地修补
Verifier.check
方法:
def do_nothing(*args, **kwargs):
pass
verifier_class.Verifier.check = do_nothing
... so you don't even have to clutter your methods up with another fixture or whatever. ...因此您甚至不必将您的方法与另一个夹具或其他东西混为一谈。 Obviously you can do this on a module-by-module basis so, as I said, some modules might choose not to do this (integration tests, etc.)
显然,您可以逐个模块地执行此操作,因此,正如我所说,某些模块可能会选择不执行此操作(集成测试等)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.