如何确定 PHPUnit 测试是否正在运行? [英] How to Determine if PHPUnit Tests are Running?

查看:51
本文介绍了如何确定 PHPUnit 测试是否正在运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个问题,我必须在遗留代码中解决问题才能使我们与 PHP 扩展的交互正常工作(单例测试题).

I currently have a problem that I have to work around in legacy code to get our interaction with a PHP Extension to work properly (Singleton Testing Question).

因此,当使用应用程序运行我们的正常生产代码时,我不想执行此代码.因此,如果正在执行的代码作为测试的一部分执行,我需要检查常规 PHP 代码.

As such, I do not want to execute this code when running our normal production code with the application. Therefore, I need to check in regular PHP code if the code being executed is being executed as part of a test or not.

关于如何确定这一点的任何建议?我想到了一个与测试文件本身存在相关的已定义变量(我们不会将测试发送给客户),但我们的开发人员需要扩展才能正常工作,而 CI 服务器需要运行测试.

Any suggestions on how to determine this? I thought about a defined variable tied to the presence of the test files themselves (we do not ship the tests to customers) but our developers need the Extension to work normally, while the CI server needs to run the tests.

是否建议在 PHPUnit.xml 文件中设置全局?其他想法?

Would a Global set in the PHPUnit.xml file be recommended? Other thoughts?

推荐答案

在 PHPUnit bootstrap.php 文件中定义一个常量.这是在加载或运行任何测试之前执行的.这不应该影响正常运行应用程序的开发人员 - 只是单元测试.

Define a constant in your PHPUnit bootstrap.php file. This is executed before loading or running any tests. This shouldn't impact developers running the application normally--just the unit tests.

这篇关于如何确定 PHPUnit 测试是否正在运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆