使用php cli创建测试环境
我想在控制台模式下使用 php 并创建一个环境来测试我的功能。
我不想每次想要测试某个功能时都被迫使用网络浏览器并创建一个新文件。
我想访问控制台中的函数,然后返回结果。
我该怎么做呢?
更新:
也许我对此解释得很糟糕。 我只想看看函数的返回结果是什么。
也许我必须学习单元测试,但目前我只想要一个交互式控制台,它允许我一一测试所有功能。
就我而言,我必须加载 WordPress 函数(我知道如何使用常规 .php
文件,然后使用浏览器来解析该文件),但如果可以的话,我不知道从命令行使用 php。
I want to use php in console mode and create an environment to test my functions.
I do not want to be forced to use a web browser and create a new file each time I want to test a function.
I want to access the function in the console and then it return the result.
How do I do this?
Update:
Perhaps I have explained this badly. I only want to see what result the function's return.
Maybe I have to learn unit testing but for the moment I only want an interactive console which allows me to test all functions one by one.
In my case I have to load the wordpress functions (I know how do it with a regular .php
file and then a browser to parse the file) but i don't if it is possible to do it with php from the command line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我过去使用过 phpsh ,发现它非常有用。 一旦启动它,您将需要
chdir()
到您的文件所在的位置,然后显然require()
任何包含您需要测试的函数的文件。 然后,您可以通过在 shell 中输入函数调用来测试它们,例如var_dump(some_function(1, 2));
I have used phpsh in the past and found it very useful. Once you start it you will need to
chdir()
to where your files are and then obviouslyrequire()
any files containing functions you need to test. You can then just test your function calls by typing them into the shell e.g.var_dump(some_function(1, 2));
我想你必须更具体地说明到底是什么类型的功能。 Wordpress 不提供类似的开箱即用的功能,大多数 PHP 应用程序也不会。
我还认为,当此类应用程序没有考虑到此类环境而开发时,您就是在自找麻烦。
这是一个尝试从functions.php调用“current_time()”的示例,我必须做的尝试才意识到它不会以这种方式工作:
给出
尝试
给出
尝试
给出
查看源中的最后一个错误我看到
使用全局变量使得在其他环境中进行测试成为 PITA(如果不是不可能的话)。
如果这不是您想要做的,那么您的问题必须更加具体/详细。
I guess you've to be more specific what kind of functions exactly. Wordpress does not provide something like that out of the box, most PHP apps won't.
I also think you're calling for trouble here when such apps aren't developed in mind for such environments.
Here's an example trying to call "current_time()" from functions.php and the attempts I had to do just to realize it won't work that way:
gives
Trying
gives
Trying
gives
Looking at the last error in the source I see
Using global variables makes testing in other environments a PITA if not impossible.
If this is not what you're trying to do, you've to be more specific/detailed in your question.
您将需要阅读一般意义上的“单元测试”,然后尝试将它们应用到 PHP 中。
您正在使用的框架(如果有)、代码风格以及您想要运行的测试将决定您需要使用的确切方法。 只有首先理解单元测试的概念并将其实施到您的编码最佳实践中,您才能在这方面取得进展。
You are going to want to read up on "Unit Testing" in a generic sense and then try and apply them to PHP.
The framework you are using (if any), the style of code, and the tests you want to run are going to determine the exact methods you need to use. Only by first understanding the concept of Unit Tests and implementing them into your coding best-practices will you be able to make progress in this regard.
怎么样:
如果你用 readline 支持来编译 php,它会更奇特。
How about:
And if you compile php with readline support it'll be more fancy.