自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用Cucumber在Android中實(shí)現(xiàn)行為驅(qū)動開發(fā),你學(xué)會了嗎?

移動開發(fā) Android
軟件開發(fā)涉及流程和人員。人員包括技術(shù)和非技術(shù)利益相關(guān)者,但由于流程主要是技術(shù)性的,因此它會在技術(shù)和非技術(shù)利益相關(guān)者之間造成很大的差距。

通過使用人類語言,行為驅(qū)動開發(fā)(BDD)彌合了軟件開發(fā)中技術(shù)人員和非技術(shù)人員之間的差距。

譯自Implement Behavior-Driven Development in Android With Cucumber,作者 Stephen Henry。

軟件開發(fā)涉及流程和人員。人員包括技術(shù)和非技術(shù)利益相關(guān)者,但由于流程主要是技術(shù)性的,因此它會在技術(shù)和非技術(shù)利益相關(guān)者之間造成很大的差距。

彌合這一差距需要一種協(xié)作方法,該方法使用自然語言來鼓勵技術(shù)和非技術(shù)利益相關(guān)者之間的溝通和協(xié)作。這正是行為驅(qū)動開發(fā) (BDD)的目標(biāo):在開發(fā)人員、測試人員和業(yè)務(wù)利益相關(guān)者之間達(dá)成理解。

作為一名軟件工程師,我一直在使用最流行的工具之一Cucumber來實(shí)現(xiàn) BDD。Cucumber 通過協(xié)作執(zhí)行規(guī)范來幫助業(yè)務(wù)和技術(shù)團(tuán)隊(duì)協(xié)作。BDD 規(guī)范還兼作自動化測試。使用 Gherkin 框架,這些規(guī)范是協(xié)作編寫的,使團(tuán)隊(duì)與系統(tǒng)的實(shí)時文檔保持一致。

在本文中,我將解釋將 Cucumber 測試集成到Android 應(yīng)用程序中的五個簡單步驟。

  • 有 Java 虛擬機(jī) (JVM) 語言。
  • 它與 Espresso 框架無縫集成,用于用戶界面 (UI) 測試。
  • 它使任何人都可以使用任何口語編寫所需行為的純文本描述,并使用這些描述運(yùn)行自動化測試。它的純語言解析器 Gherkin 促進(jìn)了這一點(diǎn),因?yàn)樗钥蛻?、利益相關(guān)者、經(jīng)理、開發(fā)人員、質(zhì)量保證 (QA) 測試人員等可以理解的清晰且合乎邏輯的語言指定了預(yù)期的軟件行為。
  • 它提供了有關(guān)應(yīng)用程序的出色文檔。
  • 它可以使用 BDD 運(yùn)行自動驗(yàn)收測試。

使用 Cucumber 設(shè)置 Android Studio 進(jìn)行測試

讓我們深入了解如何使用 Cucumber Tests 設(shè)置 Android Studio。

先決條件

在開始之前,請確保已安裝Android Studio集成開發(fā)環(huán)境 (IDE)。

您還可以考慮從 Android Studio 市場安裝以下插件:

  • Cucumber 對 Kotlin 和 Android 的支持:Finanteq 提供的這個插件使得可以使用 Kotlin 編寫步驟定義來支持 Cucumber。它允許直接從 IDE 運(yùn)行 Cucumber 場景作為 Android 工具化測試。
  • JetBrains 的 Gherkin 和 Cucumber 支持 Java:這些插件增加了對 Gherkin 語言的支持,Cucumber 測試工具使用這種語言,并為步驟定義提供編碼輔助。

1. 創(chuàng)建帶有依賴項(xiàng)的 Android Studio 項(xiàng)目

在 IDE 中創(chuàng)建一個新的 Android Studio 項(xiàng)目,或使用現(xiàn)有項(xiàng)目。接下來,添加 Cucumber 依賴項(xiàng)。

在 app 級模塊中 build.gradle 文件中,添加以下依賴項(xiàng):\

androidTestImplementation("io.cucumber:cucumber-android:7.14.0")
androidTestImplementation ("androidx.test:rules:1.6.1")

2. 創(chuàng)建您的 Instrumentation Runner

在 app/src/androidTest/java/com/your/app/ 中,創(chuàng)建一個名為 CucumberTestInstrumentation.java 的自定義 Instrumentation Runner。將此類添加到build.gradle 中的 android > defaultConfig 下:

defaultConfig {
   .....
   testInstrumentationRunner "com.your.app.CucumberTestInstrumentation"
 
}

您已成功為 Cucumber 設(shè)置 Android Studio,因此現(xiàn)在可以繼續(xù)進(jìn)行激動人心的部分。

3. Given、When、And 和 Then

Gherkin 是一種特定于領(lǐng)域的語言,它使用非技術(shù)術(shù)語逐步描述功能的實(shí)現(xiàn)。它使用關(guān)鍵字 Given、When、And 和 Then 來解釋步驟。這些步驟可以用任何人類語言編寫,例如英語、阿拉伯語或盧奧語。

以下是我將在此項(xiàng)目中使用的用英語編寫的 Gherkin 特性場景示例:

Feature: Ability of the customer to login
Scenario Outline: Where the customer requests to log in
Given the login screen is displayed to the customer
When the customer enters email "<email>"
And the customer enters password "<password>"
And the customer requests to log in
Then the login status will be "<status>"
Examples:
| email                 | password   | status        |
| someemail@gmail.com   | qwerty1234 | Login Failed  |
| example@gmail.com     | somePass02 | Login Failed  |
| stevehechio@gmail.com | sayNerd001 | Login Success |

在 app/src/androidTest/assets 中創(chuàng)建一個 assets 目錄,并添加一個名為 features 的文件夾。您將在其中添加包含用英語編寫的上述步驟定義的功能文件。

添加一個名為 login.feature 的新 .feature 文件,并添加上述功能步驟。

4. 使用 Espresso 框架實(shí)現(xiàn)場景步驟

在 app/src/androidTest/java/com/your/app/ 中,創(chuàng)建一個名為 LoginSteps 的 Kotlin 類。您將在其中編寫測試來實(shí)現(xiàn) login.feature 中的步驟。

以下是步驟實(shí)現(xiàn)的代碼片段:

@Given("^the login screen is displayed to the customer$")
fun theLoginScreenIsDisplayedToTheCustomer() {
   composeTestRule.setContent {
      LoginScreen()
   }


   composeTestRule.onNodeWithTag(LOGIN_SCREEN).assertIsDisplayed()
   composeTestRule.onNodeWithTag(LOGIN_SCREEN_EMAIL).assertIsDisplayed()
   composeTestRule.onNodeWithTag(LOGIN_SCREEN_PASSWORD).assertIsDisplayed()
   composeTestRule.onNodeWithTag(LOGIN_SCREEN_BUTTON).assertIsDisplayed()
   composeTestRule.mainClock.advanceTimeBy(500)
}


@When("^the customer enters email \"([^\"]*)\"$")
fun theCustomerEntersEmail(email: String) {
       composeTestRule.onNodeWithTag(LOGIN_SCREEN_EMAIL).performTextInput(email)
       composeTestRule.mainClock.advanceTimeBy(500)
       composeTestRule.onNodeWithTag(LOGIN_SCREEN_EMAIL).assertExists().assert(hasText(email))
}

5. 提供 Cucumber 選項(xiàng)

運(yùn)行測試時,您必須提供包含步驟定義的包,并將它們粘貼到步驟中。

在app/src/androidTest/java/com/your/app中,創(chuàng)建一個名為test的文件夾,并添加一個新的 Kotlin 類。

@RunWith(Cucumber::class)
@CucumberOptions(
features = ["src/androidTest/assets/features/login.feature"],
glue = ["com.stevehechio.apps.hechiobdd.LoginSteps"],
monochrome = true
)
class HechioCucumberTestRunner

最后,您可以運(yùn)行測試,但首先,確認(rèn)您的項(xiàng)目結(jié)構(gòu)如下所示:

圖片圖片

運(yùn)行測試

要運(yùn)行測試:

  • 打開編輯配置。
  • 單擊  左面板上的+,然后選擇 Android Instrumented Tests。
  • 編寫名稱以匹配功能的名稱,以便于記憶。在這種情況下,即客戶登錄的能力。然后單擊運(yùn)行或確定以稍后從 IDE 工具欄運(yùn)行或調(diào)試它。

圖片圖片

以下是上述實(shí)現(xiàn)的結(jié)果。

圖片圖片

結(jié)論

彌合技術(shù)和非技術(shù)利益相關(guān)者之間的差距對于有效的軟件開發(fā)至關(guān)重要。行為驅(qū)動開發(fā)促進(jìn)了自然語言中的協(xié)作和溝通。

Cucumber 等工具可幫助使用可執(zhí)行規(guī)范作為自動化測試來實(shí)現(xiàn) BDD,從而使團(tuán)隊(duì)能夠創(chuàng)建清晰、共享的文檔。將 Cucumber 集成到您的 Android 應(yīng)用程序開發(fā)中可以增強(qiáng)團(tuán)隊(duì)協(xié)調(diào)并簡化開發(fā)。

責(zé)任編輯:武曉燕 來源: 云云眾生s
相關(guān)推薦

2024-01-29 08:21:59

AndroidOpenCV車牌

2025-01-26 15:31:27

2024-10-16 11:28:42

2022-06-16 07:50:35

數(shù)據(jù)結(jié)構(gòu)鏈表

2024-02-02 11:03:11

React數(shù)據(jù)Ref

2024-01-16 08:22:42

Gradient線性梯度

2022-03-05 23:29:18

LibuvwatchdogNode.js

2021-10-31 20:07:49

Windows驅(qū)動開發(fā)

2022-04-29 08:55:43

前端開發(fā)規(guī)范

2024-10-09 07:40:43

2024-07-29 10:35:44

KubernetesCSI存儲

2023-10-30 07:05:31

2023-12-27 07:31:45

json產(chǎn)品場景

2024-01-30 08:30:41

TypeScript編譯器類型

2023-01-10 08:43:15

定義DDD架構(gòu)

2024-02-04 00:00:00

Effect數(shù)據(jù)組件

2023-07-26 13:11:21

ChatGPT平臺工具

2024-01-19 08:25:38

死鎖Java通信

2024-01-02 12:05:26

Java并發(fā)編程

2023-08-01 12:51:18

WebGPT機(jī)器學(xué)習(xí)模型
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號