简体   繁体   English

pytest或python中的任何测试工具能否自动生成单元测试?

[英]Can pytest or any testing tool in python generate the unit test automatically?

I started writing pytest for the source code I have.我开始为我拥有的源代码编写 pytest。 For this I see the code and in test I check if that piece of code returns expected value.为此,我查看代码并在测试中检查该段代码是否返回预期值。 Is there any way to automatically generate test cases for python code, positive and parameterized so that it doesn't take much time if the source code is huge有没有办法自动生成python代码的测试用例,正向和参数化,这样如果源代码很大也不会花太多时间

You can't generate test cases using Pytest, but you can use tools like hypothesis to randomise the particular values that your test cases are given.您无法使用 Pytest 生成测试用例,但您可以使用诸如假设之类的工具来随机化为测试用例提供的特定值。 You still need to specify what cases you want to test though, as well as giving a definition for how it should generate the parameters.不过,您仍然需要指定要测试的情况,并定义它应该如何生成参数。

Generally, computers aren't smart enough to know what we want a function to do, so there's no way they can tell whether we intentionally added certain behaviours into our code or whether they are just bugs.通常,计算机不够聪明,无法知道我们想让 function 做什么,因此它们无法判断我们是否有意将某些行为添加到我们的代码中,或者它们是否只是错误。 Tools like mypy and flake8 can help catch questionable code, but it's still up to you whether that code is correct or not - they can't know for certain.mypyflake8这样的工具可以帮助捕获有问题的代码,但代码是否正确仍然取决于你——它们无法确定。

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

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