# Unity 單元測試目錄規範與建立 ## 插件目錄與單元測試目錄 ### 以插件為例: ![image](https://hackmd.io/_uploads/SJk8EI8-R.png) ### 於插件中建立單元測試目錄: ![image](https://hackmd.io/_uploads/Sy6gEU8bR.png) --- ## 建立 Tests Assembly ### 依照插件名稱對應創建 Tests Assembly Editor.Tests (**PluginMaker.Editor.Tests = 插件名稱.Editor.Tests**)。 ![image](https://hackmd.io/_uploads/BkN8BLLZ0.png) Runtime.Tests (**PluginMaker.Runtime.Tests = 插件名稱.Runtime.Tests**)。 ![image](https://hackmd.io/_uploads/HJQqBU8bA.png) --- ## 配置 Tests Assembly ### Editor.Tests ![image](https://hackmd.io/_uploads/BJUB_LI-C.png) ### Runtime.Tests Define Constraints **(透過宏的限制,以防編譯)** ``` // 可自定義 UNITY_INCLUDE_TESTS ``` 另外,如果有部分測試代碼想要編譯,如下: ``` #if UNITY_INCLUDE_TESTS // 允許編譯的 Tests 代碼 #endif ``` ![upload_be74c91694e211499423831a657862ca](https://hackmd.io/_uploads/B1BfmW5o0.png) --- ## 開始編寫簡單的單元測試 ### 建立 Tests 腳本 ![image](https://hackmd.io/_uploads/r1Tdu8IZ0.png) ### 建議腳本以 Tests 結尾 -> MyFirst**Tests**.cs ![image](https://hackmd.io/_uploads/ry1xtLL-R.png) ![image](https://hackmd.io/_uploads/ry7oYLLbC.png) --- ## 執行單元測試 ### 開啟 Test Runner ![image](https://hackmd.io/_uploads/HJlsT8LWC.png) ### 運行測試結果 ![image](https://hackmd.io/_uploads/ryM6pUIbC.png)