Setup的StartService步驟,如果被測試對象是一個影子進程,而你測試的是這個影子進程的配置項,那么每次測試的時候,你就都需要重新啟動這個進程,以確最新的配置項被加載。
Run的sendInput階段,有些被測試程序是被動接收數據的,比如一個http服務,或者一個監控某一個目錄的影子進程,那么這樣的被測對象,要啟動它,只需要將數據發送給他就好了。所以這個步驟就是用于發送測試數據。
Run的execute階段,大部分被測試對象是屬于需要主動運行的,比如hadoop或者hive腳本,需要執行一個腳本,來啟動被測試對象,那么這種啟動的代碼,放在這個步驟中。
Run的getResult階段,
Teardown的stopService階段,用來關閉被測試對象的影子服務。
Teardown的stopRefService階段,用來關閉之前啟動的被測試對象依賴的服務。
以上的階段和步驟的劃分,只是對整個自動化測試過程的一個劃分,針對不同的被測試對象,步驟執行也是不同的,并不是每一個步驟的需要有代碼,而是選擇最合適的步驟就好。
按照這個思路,我們改造之前的代碼:
class underTest
{
function go()
{
$this->setup();
$this->run();
$this->tearDown();
}
function setup()
{
$this->cleanEvironment();
$this->generateConfig();
$this->generateData();
$this->startRefServices();
$this->startService();
}
function run()
{
$this->sendInput();
$this->execute();
$this->getResult();
}
function teardown()
{
$this->stopService();
$this->stopRefServices();
}
function cleanEvironment()
{
exec('rm -rf /table_path/staff');
}
function generateConfig(){}
function gnerateData()
{
$input = get_user_input();
file_put_contents('/tmp/staff.table', $input); //將輸入的數據寫入到文件
}
function startRefServices(){}
function startService(){}
function sendInput()
{
exec('hadoop fs -mkdir /table_path/staff/'); //在hadoop上建立目標文件的目錄路徑
exec('hive -e "create external table stuff(name string, age bigint, salary bigint) partitioned by (dp string) location \'/table_path/staff/dp=etao\';"'); //建立hive的建表語句
exec('hadoop fs -put /tmp/staff.table/table_path/staff/dp=etao/part-000'); //將我們的數據文件放到hdfs上
exec('hive -e "alter table staff add if not exists partition ( dp=etao ) location \’/table_path/staff/dp=etao"');//調用alter table為我們的表增加一個分區,地址是剛剛上傳的文件(hive的專有特性)
}
function execute()
{
exec('underTestShell.sh'); //執行被測試腳本
}
function getResult()
{
exec('hadoop fs -cat /tmp/result.table/table_path/result/dp=etao/part-000 > /tmp/result.tmp'); //將結果表的結果下載下來
$result = file_get_contents('/tmp/result.tmp'); //結果表讀取到內存中
}
function stopService(){}
function stopRevServices(){}
}
?>
你也許會說,這不就是將我之前寫好的代碼拆成幾個步驟嗎?是的,但是這么做好處多多:
1. 規范。劃分成這樣的階段和步驟之后,測試人員就可以找到自己應該在哪個階段寫入代碼,維護其他測試人員代碼的時候,也就可以找到相應的階段了。
2. 可分步驟運行。如果測試應用定義了啟動相關服務,而測試用例運行的時候,只需要啟動一次服務,啟動之后的其他測試用例運行就無需啟動,這種情況下,我們可以在test dirver中,選擇性的運行這一步驟。其他步驟也是一樣的。
3. 更加大局的看,這么做將來可以將每個步驟需要做的工作,抽象成模塊,供給其他測試應用使用。
如果你有心,你會發現,可以用面向對象的思想將這些步驟的定義提取到一個父類去,然后以后只需要繼承就好。
在接下來的一章,我們將會講對于數據的抽象和封裝。
//下面一段是期望操作
$input = "zhangsan|28|8000\n";//準備輸入數據,第一行
$input.= "lisi|30|10000\n"; //準備輸入數據,第二行
$input.= "wangwu|40|20000\n"; //準備輸入數據,第三行
//下面一段是無關操作
file_put_contents('/tmp/staff.table', $input); //將輸入的數據寫入到文件
exec('hadoop fs -mkdir /table_path/staff/'); //在hadoop上建立目標文件的目錄路徑
exec('hive -e "create external table stuff(name string, age bigint, salary bigint) partitioned by (dp string) location \'/table_path/staff/dp=etao\';"'); //建立hive的建表語句
exec('hadoop fs -put /tmp/staff.table/table_path/staff/dp=etao/part-000'); //將我們的數據文件放到hdfs上