負責加載測試腳本檔案,子產品代碼路徑./src/robot/parsing/
一、解析邏輯
我們以keyword_driven.robot測試腳本為例,入口點是./src/robot/parsing/model.py的
TestData()
函數,分析其中的解析過程。
1.1 表格解析器
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiQ3chVEa0V3bT9CX5RXa2Fmcn9CXwczLcVmds92czlGZvwVP9EUTDZ0aRJkSwk0LcxGbpZ2LcBDM08CXlpXazRnbvZ2LcRlMMVDT2EWNvwFdu9mZvwFNVNTWtZEWiZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39zMzczN0ETMwIjMxATM3EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
-
構造函數中會執行個體化四個表格對象:設定表格、變量表格、測試用例表格、使用者關鍵字表格,并在TestCaseFile
函數中調用populate()
;FromFilePopoulator(self).populate(self.source)
- 打開腳本檔案,并從路徑中擷取字尾名,再到
字典擷取到對應的檔案格式讀取器;READERS
-
繼承了TxtReader
,讀取出檔案中的每一行;TsvReader
-
根據定義的格式将一行字元劃分成列(cells),其分隔符為兩個既以上的空格,“|”也是分隔符;TxtReader
-
會區分cells中的注釋部分和與非注釋部分;DataRow
-
中判斷TsvReader
是否為星号“*”字元開始,以此解決用解析到時的關鍵字去擷取相應的表格對象;cell[0]
- 通過表格對象type屬性查找出對應的解析器,指派給
,作為目前表格解析器;FromFilePopulator._populator
- 如果
不是星号“*”字元開始,則調用目前表格解析器的cell[0]
方法。add()
1.2 表格内解析器
- 檔案格式讀取器解析出每一行的每一列;
-
判斷第一列不為連續行辨別“…”,便用名稱從DataRow
找到Documentation相應對象;ResourceFileSettingTable
-
對象建立的表格内解析器Documentation
;DocumentationPopulator
- 本行後續列調用
方法添加;DocumentationPopulator.add()
- 若第一列為連續行辨別,則繼續使用上一次的解析器,調用
方法,直到不是連續行辨別為止;add()
- 解析到設定項Library,同樣從
找到相應對象ResourceFileSettingTable
;ImportList
- 建立相應的表格内解析器
,替換成為目前解析器;SettingPopulator
- 同時,調用原解析器對應的設定項對象的
方法,将其解析器收集到的所有列合并成為Documentation.populate()
對象的value。Documentation
此時,完整的表格内設定項的内容讀取完成,其中的設定項類似。
二、組織結構
腳本檔案會生成
TestCaseFile
對象,根據腳本中各區域形成四個表對象,頂級解析器是
FromFilePopulator
,包含了對應的四個解析器,見下表
區域塊 | 表對象 | 解析器 |
---|---|---|
Setting | Settings | TestCaseFileSettingTable | SettingTablePopulator |
Variable | Variables | VariableTable | VariableTablePopulator |
Test Case | Test Cases | TestCaseTable | TestTablePopulator |
Keyword | Keywords | KeywordTable | KeywordTablePopulator |
2.1、設定表
關鍵字 | 對象 | 解析器 |
---|---|---|
Documentation | Documentation | DocumentationPopulator |
Library | Library | SettingPopulator |
Resource | Resource | SettingPopulator |
Variables | Variables | SettingPopulator |
Metadata | MetadataList | MetadataPopulator |
Suite Setup | Fixture | SettingPopulator |
Suite Teardown | Fixture | SettingPopulator |
Force Tags | Tags | SettingPopulator |
Default Tags | Tags | SettingPopulator |
Test Setup | Fixture | SettingPopulator |
Test Teardown | Fixture | SettingPopulator |
Test Template | Template | SettingPopulator |
Test Timeout | Timeout | SettingPopulator |
2.2、變量表
表對象為
VariableTable
,解析器為
VariableTablePopulator
,表内的解析器
VariablePopulator
将腳本中的每個變量解析成Variable對象添加到
VariableTable
的variables清單。這裡對$變量、@清單、&字典類型并無差別處理。
2.3、測試用例表
表對象為
TestCaseTable
,解析器為
TestTablePopulator
,表内的解析器
TestCasePopulator
将腳本中的每個測試用例解析成TestCase對象添加到
TestCaseTable
的tests清單。其中測試用例中的每個action會組合成資料一塊放入同一個TestCase對象。表内解析器會調用
TestCasePopulator
- 根據“[]”字元選擇不同的關鍵字解析器,如下表,複用了設定表中的類
- 根據“:FOR”字元會選擇
解析器,解析循環語句,在TestCase對象的steps清單中添加ForLoop對象:拆分為變量、FOR和集合部分,FOR後續語句需要用”\“字元使DataRow對象解析成縮進ForLoopPopulator
- 否則使用
解析器,解析後續語句為Step對象添加到steps清單:拆分為指派變量清單、關鍵字和參數清單,解析過程:如果第一列是變量(除掉指派符)則把該列添加到指派變量清單裡,并彈出該列後疊代判斷,接下來的一列為關鍵字,後面的放入參數清單。StepPopulator
關鍵字 | 對象 | 解析器 |
---|---|---|
[Documentation] | Documentation | DocumentationPopulator |
[Template] | Template | SettingPopulator |
[Tags] | Tags | SettingPopulator |
[Setup] | Fixture | SettingPopulator |
[Teardown] | Fixture | SettingPopulator |
[Timeout] | Timeout | SettingPopulator |
2.4、關鍵字表
表對象為
KeywordTable
,解析器為
KeywordTablePopulator
,表内的解析器
UserKeywordPopulator
将腳本中的每個測試用例解析成UserKeyword對象添加到
KeywordTable
的keywords清單。表内解析器調用
UserKeywordPopulator
,與測試用例邏輯一樣,因為都繼承于
_TestCaseUserKeywordPopulator
,支援“[]”、“:FOR”、普通語句,其中支援的關鍵字如下表
關鍵字 | 對象 | 解析器 |
---|---|---|
[Documentation] | Documentation | DocumentationPopulator |
[Arguments] | Arguments | SettingPopulator |
[Return] | Return | SettingPopulator |
[Timeout] | Timeout | SettingPopulator |
[Teardown] | Fixture | SettingPopulator |
[Tags] | Tags | SettingPopulator |