一个自动生成PHPUnit测试的工具?

时间:2022-10-15 22:48:06

I was wondering - is there a tool which will look at my PHP code and automatically generate a PHPUnit test for it? Or is there a tool like this for any other language, which I might be able to port to PHP?

我想知道-是否有一个工具可以查看我的PHP代码并自动为它生成PHPUnit测试?或者其他语言有这样的工具,我可以移植到PHP吗?

I'm not talking about generating a skeleton. I was thinking that it would be possible for a tool to look at tokenized PHP and determine the code paths through a method, and then automatically generate a test for each code path, creating mocks and setting "expects" calls as necessary.

我不是说生成骨架。我认为工具可以查看标记化的PHP并通过一个方法确定代码路径,然后自动为每个代码路径生成一个测试,创建mock并在必要时设置“expect”调用。

Even if there's no tool that currently does it, might such a task be feasible or am I overlooking something?

即使目前还没有工具可以完成这个任务,但这个任务是否可行,还是我忽略了什么?

2 个解决方案

#1


4  

Turns out there is no tool that does this currently, so I wrote one (with help from my colleagues). Not complete - just enough to explore the concept. By using nikic's PHP-Parser(https://github.com/nikic/PHP-Parser), it's possible to find all the method calls in the methods of a class, and then mocks can be created for them.

现在还没有工具可以做到这一点,所以我写了一个(在同事的帮助下)。不完整-仅仅足够探究这个概念。通过使用nikic的php解析器(https://github.com/nikic/PHP-Parser),可以在类的方法中找到所有方法调用,然后可以为它们创建mock。

At the very least, I might eventually get it to the point where I can use it to make some fill-in-the-blank unit tests.

至少,我可能最终会把它用到我可以用它做一些填空单元测试的地方。

#2


2  

Unlikely to be feasible.

可能是可行的。

Current techniques for automatic test generation depend on the presence of specifications for the code's behavior, not just an implementation. Inferring a spec from PHP code is not necessarily easy for a human, let alone a machine, so I seriously doubt that it'd be possible to make this work for any real-world code.

当前自动生成测试的技术依赖于代码行为规范的存在,而不仅仅是实现。从PHP代码中推断一个规范对人类来说并不容易,更不用说机器了,因此我非常怀疑是否有可能将它用于任何真实的代码。

#1


4  

Turns out there is no tool that does this currently, so I wrote one (with help from my colleagues). Not complete - just enough to explore the concept. By using nikic's PHP-Parser(https://github.com/nikic/PHP-Parser), it's possible to find all the method calls in the methods of a class, and then mocks can be created for them.

现在还没有工具可以做到这一点,所以我写了一个(在同事的帮助下)。不完整-仅仅足够探究这个概念。通过使用nikic的php解析器(https://github.com/nikic/PHP-Parser),可以在类的方法中找到所有方法调用,然后可以为它们创建mock。

At the very least, I might eventually get it to the point where I can use it to make some fill-in-the-blank unit tests.

至少,我可能最终会把它用到我可以用它做一些填空单元测试的地方。

#2


2  

Unlikely to be feasible.

可能是可行的。

Current techniques for automatic test generation depend on the presence of specifications for the code's behavior, not just an implementation. Inferring a spec from PHP code is not necessarily easy for a human, let alone a machine, so I seriously doubt that it'd be possible to make this work for any real-world code.

当前自动生成测试的技术依赖于代码行为规范的存在,而不仅仅是实现。从PHP代码中推断一个规范对人类来说并不容易,更不用说机器了,因此我非常怀疑是否有可能将它用于任何真实的代码。