简体   繁体   English

我可以在 python 优化的情况下运行 unittest / pytest 吗?

[英]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.

相关问题 python unittest和pytest-我可以将测试状态分配给变量 - python unittest and pytest - can I assign test status to a variable Python pytest unittest.TextTestRunner不运行特定的套件 - Python pytest unittest.TextTestRunner doesn't run specific suites 使用pytest在python unittest中的tearDown中运行的注册方法? - Register method to run at tearDown in python unittest using pytest? Python中的Unittest可以运行由字符串组成的列表吗? - Can Unittest in Python run a list made of strings? 如何在需要告诉测试文件所在位置的地方运行python unittest? - How can I run a python unittest where I need to tell the test where a file is? 我可以通过Python中的单元测试确定应用程序是否正常运行吗? - Can I determine if an application is being run normally vs via a unittest in Python? 我可以通过“ runpy.run_module”调用“ python -m unittest test_code”吗? - Can I call “python -m unittest test_code” by “runpy.run_module”? 如何将 unittest 子测试转换为 pytest - How do I convert a unittest subtest to pytest 我怎样才能让 pytest 忽略不继承 unittest 的 Test* 类? - How can I get pytest to ignore Test* classes that don't subclass unittest? 如何在没有 .py 扩展名的 Python 文件上运行 pytest(和 doctests)? - How can I run pytest (and doctests) on Python files without .py extension?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM