EasySwoole中進行單元測試

2022-01-26 04:09:06 字數 2794 閱讀 2909

之前寫了一篇文章《phpunit的安裝以及在phpstorm中的使用》,講述如何在ide中安裝phpunit進行簡單的單元測試。但是單是這樣往往是不夠的,實際專案中在進行測試之前,還需要初始化很多配置資訊,比如資料庫連線,核心元件的載入。今天再記錄一下在easyswoole中無需通過ide安裝phpunit的情況下是如何進行單元測試。

1、準備工作

1)生成測試檔案

2)生成的測試檔案以及目錄 unittest/user/usertest.php

這裡貼下basetest.php**:

1

<?php23

namespace unittest;45

usecurl\curl;

6use

easyswoole\easyswoole\core;

7use

easyswoole\easyswoole\logger;

8use

phpunit\framework\testcase;910

/**11* basetest

12* class basetest

13* create with classgeneration

14*/

15class basetest extends

testcase

16 else

38$this->asserttrue(!!$curl->response);

39$this->assertequals(200, $curl->response->code, $curl->response->msg);

40return

$curl->response;41}

4243/**

44* 初始化配置以及預載入各核心元件

45* @return bool|void

46*/

47public

function

setup()

4853

require_once

dirname(__file__, 2) . '/vendor/autoload.php';

54defined('easyswoole_root') or define('easyswoole_root', dirname(__file__, 2));

55require_once

dirname(__file__, 2) . '/easyswooleevent.php';

56 core::getinstance()->initialize()->globalinitialize();

57 self::$isinit = 1;

58 logger::getinstance()->info(">>>>>>>>>isinit:".self::$isinit

);59

$this->curl = new

curl();60}

61 }

view code

注意request()這個方法

api/user/user.php (控制器)

api/base.php(user.php繼承的父類)

2、在linux上執行easyswoole

這裡開啟兩個shell,

1)其中乙個執行easyswoole

2)另外乙個執行phpunit命令

這裡執行php easyswoole phpunit  unittest/

那麼如何解決呢?由於easyswoole文件中對於phpunit的命令講解比較少

我們可以在easyswoole控制台執行:php easyswoole phpunit檢視各引數用法

然後這裡看完,我們心裡就應該明白,問題出在需要加上--bootstrap這個引數,它就是用來在測試執行前把檔案載入進去的

我們再執行:php  easyswoole  phpunit  --bootstrap  unittest/basetest.php   unittest/

結果正常,符合預期

C 中進行單元測試

首先建立乙個專案,寫一段待測的程式 namespace fortest public int say int a,int b else 然後滑鼠右鍵點選say函式,選擇create unit tests,這裡我寫了兩個case using microsoft.visualstudio.testtoo...

Makefile中進行巨集定義

實際上是gcc命令支援 d巨集定義,相當於c中的全域性 define gcc d name gcc d name definition makefile中可以定義變數 和巨集很像 但是是給make直譯器用的,對所編譯的檔案完全沒有作用。mstar 巨集定義方式 字串定義方式和值定義方式 cc tvo...

在VBA中進行除錯

只要從您的專案中觸發 vba就會執行。從簡單的開啟表單到單擊命令按鈕,各種各樣的物件事件都可能引起這種情況。當某些事情無法按預期工作時,您如何才能準確確定出什麼問題以及在 好吧,這就是本文可以為您提供的幫助。對於每個office應用程式,當然包括access,都有乙個整合的偵錯程式。要開啟偵錯程式視...