NativeScript - 測試



測試是應用程式開發生命週期中非常重要的一個階段。它確保應用程式的質量。它需要仔細的計劃和執行。它也是開發中最耗時的階段。NativeScript 框架為應用程式的自動化測試提供了廣泛的支援。

測試型別

通常,有三種類型的測試流程可用於測試應用程式。它們如下所示:

單元測試

單元測試是測試應用程式最簡單的方法。它基於確保程式碼片段(通常是函式)或類方法的正確性。但是,它沒有反映真實環境,因此。它是查詢錯誤的最佳選擇。

通常,NativeScript 使用 Jasmine、Mocha with Chai 和 QUnit 單元測試框架。

要執行此操作,首先需要使用以下命令在專案中進行配置:

tns test init

現在,您將收到以下響應:

? Select testing framework: (Use arrow keys) 
> jasmine 
   mocha 
   qunit

現在,選擇jasmine框架,您的螢幕將類似於此:

? Select testing framework: jasmine 
+ karma@4.4.1 
added 90 packages from 432 contributors and audited 11944 packages in 8.753s 

+ karma-nativescript-launcher@0.4.0 
added 2 packages from 1 contributor and audited 11946 packages in 7.299s 

> core-js@2.6.11 postinstall 
/Users/workspace/NativeScript/NativeApp/node_modules/core-js 

> node -e "try{require('./postinstall')}catch(e){}" 
Thank you for using core-js ( https://github.com/zloirock/core-js ) for 
polyfilling JavaScript standard library! 
The project needs your help! Please consider supporting of core-js on Open 
Collective or Patreon:

> https://opencollective.com/core-js 
> https://www.patreon.com/zloirock 
Also, the author of core-js ( https://github.com/zloirock ) is looking for a 
good job -) 
npm WARN karma-webpack@3.0.5 requires a peer of webpack@^2.0.0 
|| ^3.0.0 but none is installed. You must install peer dependencies yourself. 

+ karma-webpack@3.0.5 
added 19 packages from 52 contributors and audited 12012 packages in 9.368s 

+ karma-jasmine@2.0.1 
added 2 packages from 35 contributors and audited 12014 packages in 6.925s 

+ karma@4.4.1 
updated 1 package and audited 12014 packages in 7.328s 
+ @types/jasmine@3.4.6 

> nativescript-unit-test-runner@0.7.0 postinstall /Users/deiva/workspace/NativeScript/NativeApp/node_modules/nativescript-unit
-test-runner 

> node postinstall.js 
+ nativescript-unit-test-runner@0.7.0 

added 1 package from 1 contributor and audited 12032 packages in 7.14s 
Successfully installed plugin nativescript-unit-test-runner. 

Example test file created in src/tests 
Run your tests using the "$ tns test <platform>" command.

現在,測試檔案已建立在 src\tests\example.ts 中。

建立您的測試

讓我們在 example.ts 檔案中新增一個簡單的測試,如下所示:

describe("NativeApp test:", function() { 
   it("Check counter.", function() { 
      expect(mainViewModel.createViewModel().counter).toEqual(10); 
   }); 
   it("Check message.", function () { 
      expect(mainViewModel.createViewModel().message).toBe("10 taps left"); 
   }); 
});

這裡,

首先,檢查計數器是否等於 10,並檢查訊息是否為 10 taps left。

讓我們在下一步中執行測試。

執行您的測試

現在,使用以下命令在連線的 Android 或 iOS 裝置上執行測試:

tns test android

這將返回以下狀態:

? To continue, choose one of the following options: (Use arrow keys) 
> Configure for Cloud Builds 
   Configure for Local Builds 
   Configure for Both Local and Cloud Builds 
   Skip Step and Configure Manually

然後選擇以下選項:

? To continue, choose one of the following options: Configure for Local Builds 
Running the setup script to try and automatically configure your environment. 
These scripts require sudo permissions 
.....

要在 Android 模擬器中執行您的測試套件,請執行以下命令:

tns test android --emulator

現在,karma 伺服器準備構建並部署您的專案。

端到端 (E2E) 測試

單元測試是一個小型、簡單且快速的流程,而 E2E 測試階段涉及多個元件並協同工作,涵蓋應用程式中的流程。這無法透過單元測試和整合測試來實現。

NativeScript Appium 外掛用於執行 E2E 自動化測試。Appium 是一個用於移動應用程式的開源測試框架。要在專案中新增此框架,您必須擁有最新版本的 XCode 或 Android SDK 25.3.0 以上版本。

安裝 Appium

讓我們使用 npm 模組全域性安裝 Appium:

npm install -g appium

現在,您將看到以下響應:

npm install -g appium 
/Users/.npm-global/bin/authorize-ios -> 
/Users/.npm-global/lib/node_modules/ appium/node_modules/.bin/authorize-ios 

> appium-windows-driver@1.8.0 install 
/Users/.npm-global/lib/node_modules/ appium/node_modules/appium-windows-driver

> node install-npm.js 
Not installing WinAppDriver since did not detect a Windows system 

> core-js@2.6.11 postinstall /Users/.npm-
global/lib/node_modules/appium/node_modules/core-js 

> node -e "try{require('./postinstall')}catch(e){}" 
Thank you for using core-js ( https://github.com/zloirock/core-js ) for 
polyfilling JavaScript 
standard library! 
The project needs your help! Please consider supporting of core-js on Open Collective or Patreon: 

> https://opencollective.com/core-js 
> https://www.patreon.com/zloirock 
Also, the author of core-js ( https://github.com/zloirock ) 
is looking for a good job -) 

> appium-chromedriver@4.19.0 postinstall/Users/.npm-
global/lib/node_modules/appium/node_modules 
/appium-chromedriver 

> node install-npm.js 
............................................ 
............................................. 
+ appium@1.16.0 
added 671 packages from 487 contributors in 28.889s

新增外掛

讓我們將nativescript-dev-appium外掛作為開發依賴項新增到您的專案中,使用以下命令:

$ npm install -D nativescript-dev-appium

執行此操作後,選擇mocha框架,您將收到類似於此的響應:

> node ./postinstall.js 
? What kind of project do you use
? javascript ? Which testing framework do you prefer? mocha 
+ nativescript-dev-appium@6.1.3

現在,檔案儲存在您的專案資料夾中。

Project Folder

構建您的裝置

讓我們使用以下命令構建 Android 裝置:

tns build android

上述命令將執行測試,應指定目標功能。如果您有 iOS 裝置,可以使用iOS裝置進行構建。

執行測試

現在,我們已經配置了裝置。讓我們使用以下命令執行我們的測試:

npm run e2e -- --runType <capability-name>

這裡,

capability-name 定義在您的應用程式e2e/config/appium.capabilities.json中。

輸出

Config

NativeScript - 結論

NativeScript 對於 Web 開發人員來說是一個很棒的移動應用程式,可以非常輕鬆地完全測試他們的應用程式,而無需付出額外的努力。開發人員可以自信地開發出外觀精美且成功的應用程式,在短時間內不會出現任何問題。

廣告

© . All rights reserved.