通过Jenkins运行phpunit测试时,如何设置$ _SERVER ['']变量 [英] How to set $_SERVER[' '] variables when running phpunit tests through Jenkins
问题描述
我正在尝试为不可能进行大量代码更改的应用程序编写单元测试.代码库中几乎所有的.php文件都使用一些$ _SERVER ['']变量,例如
I am trying to write unit tests for an application where a lot of code changes is not possible. Almost all the .php files in the code base uses some $_SERVER[''] variables like
require_once $_SERVER['DOCUMENT_ROOT'] . '/mainApi.php';
因此,现在当我不得不编写和运行PHPUnit测试用例时,我必须以某种方式设置这些变量. 目前,我正在用户环境中设置这些变量,然后进行
So now when I have to write and run PHPUnit test cases I have to somehow set these variables. At present I am setting these variables in the user environment and then doing
$_SERVER['DOCUMENT_ROOT'] = getenv('DOCUMENT_ROOT');
require_once $_SERVER['DOCUMENT_ROOT'] . '/mainApi.php';
像这样获取服务器变量工作正常.我通过命令行以$ phpunit test.php
的身份运行测试.
Getting the server variables like this is working fine. I run my tests through commandline as $ phpunit test.php
.
问题1::可以通过命令行运行phpunit测试时设置$ _SERVER变量吗?
Ques1: Is it possible to set the $_SERVER variables while running the phpunit tests through commandline?
我还必须通过Jenkins运行这些单元测试,但无法通过ANT/build文件设置这些服务器变量.
I also have to run these unit tests through Jenkins and I am not able to set these server variable through ANT/build file.
问题2:是否可以通过Jenkins中的ant构建文件或通过运行任何Shell脚本来设置这些变量,然后再通过Jenkins执行phpunit测试?
Ques2: Is it possible to set these variable through ant build file in Jenkins or by running any shell script before executing the phpunit tests through Jenkins?
我尝试通过shell脚本导出服务器变量
I tried exporting the server variable through a shell script
export DOCUMENT_ROOT=/server/path-to-root-dir
并在Jenkins的build.xml中调用该脚本
and calling that script in the build.xml in Jenkins
<export name="setEnv" description="set server var">
<exec executable="sh">
<arg value = "sumit.sh" />
</exec>
</target>
,但不起作用.有什么我可以做的设置吗? 谢谢!
but its not working. Is there any setting that I can do for this? Thanks!
推荐答案
我不确定#1,但是PHPUnit本身必须支持它.我看不到通过命令行执行此操作的任何方法.但是,如果您将当前的解决方法放入bootstrap.php
中,则不必在每次测试中都这样做.
I'm not sure about #1, but PHPUnit itself would have to support it. I don't see any way to do that via the command line. However, if you put your current workaround into bootstrap.php
you don't have to do it in each test.
对于#2, <exec>
允许您使用以下命令设置环境变量嵌套的<env>
元素.我在詹金斯(Jenkins)中使用它.
For #2, <exec>
allows you to set environment variables using nested <env>
elements. I use this in Jenkins.
<exec executable="phpunit" ...>
<env key="DOCUMENT_ROOT" value="/var/www/php"/>
</exec>
更新:通常,您创建bootstrap.php
来设置将源目录添加到包含路径,并根据需要初始化测试环境.该文件不是由PHPUnit提供的,与phpunit.xml
不同.
Update: You typically create bootstrap.php
to setup add the source directory to the include path and initialize the test environment however you need. This file isn't supplied by PHPUnit--unlike phpunit.xml
.
我将其放置在与phpunit.xml
相同的目录中,但这是因为每个项目都有一个单独的文件.它进入通常保存测试的目录中.这样,您就可以从命令行运行phpunit
,而无需告诉它如何查找那些配置文件.否则,您必须使用--bootstrap
和/或--configuration
指向它们.
I place it in the same directory as phpunit.xml
, but that's because I have a separate file for each project. It goes in the directory that holds your tests typically. This allows you to run phpunit
from the command-line without telling it how to find those configuration files. Otherwise you have to use --bootstrap
and/or --configuration
to point to them.
这是我构建典型项目的方式:
Here is how I structure a typical project:
<project-root>/
build.xml
src/
MyClass.php
test/
MyClassTest.php
phpunit.xml
bootstrap.php
这篇关于通过Jenkins运行phpunit测试时,如何设置$ _SERVER ['']变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!