在安裝 PHP7 及各類擴展的過程當中,若是你是用源碼安裝,會注意到在 make 成功以後總會有一句提示:Don't forget to run 'make test'. 這個 make test 就是運行 PHP 的自動化測試了。php
若是你剛剛安裝完 PHP7,直接運行 make test,通過漫長的等待時間以後,最終的統計結果大概是這樣的:
會出現這麼多 skip 和 fail 說明有一些擴展你沒有安裝,相關環境的配置也須要優化html
不過通常來說咱們不須要運行這麼多的測試腳本,尤爲對單一擴展而言,針對相關接口來測試就能夠了,因此咱們進一步看看如何獨立運行測試腳本api
首先打開 php 的 Makefile文件,咱們看看 make test 都作了什麼服務器
test: all @if test ! -z "$(PHP_EXECUTABLE)" && test -x "$(PHP_EXECUTABLE)"; then \ INI_FILE=`$(PHP_EXECUTABLE) -d 'display_errors=stderr' -r 'echo php_ini_loaded_file();' 2> /dev/null`; \ if test "$$INI_FILE"; then \ $(EGREP) -h -v $(PHP_DEPRECATED_DIRECTIVES_REGEX) "$$INI_FILE" > $(top_builddir)/tmp-php.ini; \ else \ echo > $(top_builddir)/tmp-php.ini; \ fi; \ INI_SCANNED_PATH=`$(PHP_EXECUTABLE) -d 'display_errors=stderr' -r '$$a = explode(",\n", trim(php_ini_scanne d_files())); echo $$a[0];' 2> /dev/null`; \ if test "$$INI_SCANNED_PATH"; then \ INI_SCANNED_PATH=`$(top_srcdir)/build/shtool path -d $$INI_SCANNED_PATH`; \ $(EGREP) -h -v $(PHP_DEPRECATED_DIRECTIVES_REGEX) "$$INI_SCANNED_PATH"/*.ini >> $(top_builddir)/tmp -php.ini; \ fi; \ TEST_PHP_EXECUTABLE=$(PHP_EXECUTABLE) \ TEST_PHP_SRCDIR=$(top_srcdir) \ CC="$(CC)" \ $(PHP_EXECUTABLE) -n -c $(top_builddir)/tmp-php.ini $(PHP_TEST_SETTINGS) $(top_srcdir)/run-tests.php -n -c $(top_builddir)/tmp-php.ini -d extension_dir=$(top_builddir)/modules/ $(PHP_TEST_SHARED_EXTENSIONS) $(TESTS); \ TEST_RESULT_EXIT_CODE=$$?; \ rm $(top_builddir)/tmp-php.ini; \ exit $$TEST_RESULT_EXIT_CODE; \ else \ echo "ERROR: Cannot run tests without CLI sapi."; \ fi
先不用管那些變量的定義,核心內容就是下面這一句php7
$(PHP_EXECUTABLE) -n -c $(top_builddir)/tmp-php.ini $(PHP_TEST_SETTINGS) $(top_srcdir)/run-tests.php -n -c $(top_builddir)/tmp-php.ini -d extension_dir=$(top_builddir)/modules/ $(PHP_TEST_SHARED_EXTENSIONS) $(TESTS);
原來自動測試是經過執行 PHP 源碼根目錄下的 run-tests.php來進行的,測試腳本通常放在 tests 文件夾下性能
咱們隨便打開 php 安裝目錄下的 ./tests/basic,裏面有一些 phpt 文件,這就是一個個的測試腳本了
測試
有些腳本中的內容很簡單,001.phpt 中的內容是這樣的優化
--TEST-- Trivial "Hello World" test --FILE-- <?php echo "Hello World"?> --EXPECT-- Hello World
下面咱們來只運行 001.phpt 試試
首先要設置一下相關環境變量,指定被測試對象 PHP,這裏樓主用的是安裝路徑下的
phpui
export TEST_PHP_EXECUTABLE=/usr/local/php7/bin/php3d
而後在 php 安裝根目錄下執行
php run-tests.php ./tests/basic/001.phpt
不出意外的話就能夠獲得下面的結果
這樣咱們之後就能夠寫一個腳原本指定要測試的 phpt,並統計出最終的結果。
OneAPM for PHP 可以深刻到全部 PHP 應用內部完成應用性能管理 可以深刻到全部 PHP 應用內部完成應用性能管理和監控,包括代碼級別性能問題的可見性、性能瓶頸的快速識別與追溯、真實用戶體驗監控、服務器監控和端到端的應用性能管理。想閱讀更多技術文章,請訪問 OneAPM 官方技術博客。
本文轉自 OneAPM 官方博客