📜  使用JUnit在Android中进行单元测试

📅  最后修改于: 2021-05-10 16:36:22             🧑  作者: Mango

进行单元测试以确保开发人员编写高质量且无差错的代码。建议在编写实际应用程序之前编写单元测试,您将事先编写测试,并且实际代码必须遵守测试所制定的设计准则。在本文中,我们将使用JUnit来测试我们的代码。 JUnit是Java应用程序的“单元测试”框架,默认情况下已包含在android studio中。它是单元和UI测试的自动化框架。它包含注释,比如@测试,@Before,@After等等,下面我们将只使用@Test注解让文章很容易理解。请注意,我们将使用Kotlin语言实施此项目。

分步实施

步骤1:创建一个新项目

要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Kotlin作为编程语言。

第2步:将依赖项添加到build.gradle文件,然后单击“立即同步”

步骤3:使用RegistrationUtil.kt文件

创建一个新的Kotlin文件RegistrationUtil并选择其类型作为对象。由于这是一个单例,因此在其他类中使用它时,我们不需要创建它的对象。它具有一个称为validRegistrationInput的函数,该函数需要三个参数usernamepasswordConfirm password。我们将使用以下测试用例,使用不同的输入集来测试此函数。

  • 用户名,密码,确认密码不能为空。
  • 密码必须至少包含两位数。
  • 密码与确认的密码匹配。
  • 不得使用用户名。
Kotlin
object RegistrationUtil {
  
    private val existingUsers = listOf("Rahul" , "Rohan")
  
    /**
     * The test cases will pass if..
     * ...username/password/confirmPassword is not empty
     * ...password is at least 2 digits
     * ...password matches the confirm Password
     * ...username is not taken
     */
    fun validRegistrationInput(
        userName : String,
        password : String,
        confirmPassword : String
    ) : Boolean {
        // write conditions along with their return statement
        // if username / password / confirm password are empty return false
        if (userName.isEmpty() || password.isEmpty() || confirmPassword.isEmpty()){
            return false
        }
        // if username exists in the existingUser list return false
        if (userName in existingUsers){
            return false
        }
        // if password does not matches confirm password return false
        if (password != confirmPassword){
            return false
        }
        // if digit count of the password is less than 2 return false
        if (password.count { it.isDigit() } < 2){
            return false
        }
        return true
    }
}


Kotlin
import com.google.common.truth.Truth.assertThat
import org.junit.Test
  
class RegistrationUtilTest {
    // Write tests for the RegistrationUtil class considering all the conditions
    // annotate each function with @Test
    // We can use backtick to write function name..
    // whatever we write in those backtick will be considered as function name
    @Test
    fun `empty username returns false`(){
        // Pass the value to the function of RegistrationUtil class
        // since RegistrationUtil is an object/ singleton we do not need to create its object
        val result = RegistrationUtil.validRegistrationInput(
            "",
            "123",
            "123"
        )
        // assertThat() comes from the truth library that we added earlier
        // put result in it and assign the boolean that it should return
        assertThat(result).isFalse()
    }
  
    // follow the same for other cases also
    // in this test if username and correctly repeated password returns true
    @Test
    fun `username and correctly repeated password returns true`() {
        val result = RegistrationUtil.validRegistrationInput(
            "Rahul",
            "123",
            "123"
        )
        assertThat(result).isTrue()
    }
  
    // in this test serName already taken returns false
    @Test
    fun `username already taken returns false`() {
        val result = RegistrationUtil.validRegistrationInput(
            "Rohan",
            "123",
            "123"
        )
        assertThat(result).isFalse()
    }
  
    // if confirm password does nt matches the password return false
    @Test
    fun `incorrect confirm password returns false`() {
        val result = RegistrationUtil.validRegistrationInput(
            "Rahul",
            "123",
            "1234"
        )
        assertThat(result).isFalse()
    }
  
    // in this test if password has less than two digits than return false
    @Test
    fun `less than two digit password return false`() {
        val result = RegistrationUtil.validRegistrationInput(
            "Rahul",
            "abcd1",
            "abcd1"
        )
        assertThat(result).isFalse()
    }
}


步骤4:建立测试类别

为了创建RegistrationUtil的测试类,请RegistrationUtil上单击鼠标右键然后单击“生成”,然后选择测试。将打开一个对话框,从对话框中选择“测试库”作为JUnit4,并将类名保留为默认名称,即RegistrationUtilTest ,然后单击“确定”。之后,将打开另一个对话框以选择目标目录,然后选择具有..app \ src \ test \的目录。因为我们的测试课没有 要求 应用程序中的任何上下文。以下是指导您创建测试类的屏幕截图。

步骤5:使用RegistrationUtilTest.kt文件

转到RegistrationUtilTest.kt文件并编写以下代码。在代码内部添加了注释,以更详细地了解代码。

科特林

import com.google.common.truth.Truth.assertThat
import org.junit.Test
  
class RegistrationUtilTest {
    // Write tests for the RegistrationUtil class considering all the conditions
    // annotate each function with @Test
    // We can use backtick to write function name..
    // whatever we write in those backtick will be considered as function name
    @Test
    fun `empty username returns false`(){
        // Pass the value to the function of RegistrationUtil class
        // since RegistrationUtil is an object/ singleton we do not need to create its object
        val result = RegistrationUtil.validRegistrationInput(
            "",
            "123",
            "123"
        )
        // assertThat() comes from the truth library that we added earlier
        // put result in it and assign the boolean that it should return
        assertThat(result).isFalse()
    }
  
    // follow the same for other cases also
    // in this test if username and correctly repeated password returns true
    @Test
    fun `username and correctly repeated password returns true`() {
        val result = RegistrationUtil.validRegistrationInput(
            "Rahul",
            "123",
            "123"
        )
        assertThat(result).isTrue()
    }
  
    // in this test serName already taken returns false
    @Test
    fun `username already taken returns false`() {
        val result = RegistrationUtil.validRegistrationInput(
            "Rohan",
            "123",
            "123"
        )
        assertThat(result).isFalse()
    }
  
    // if confirm password does nt matches the password return false
    @Test
    fun `incorrect confirm password returns false`() {
        val result = RegistrationUtil.validRegistrationInput(
            "Rahul",
            "123",
            "1234"
        )
        assertThat(result).isFalse()
    }
  
    // in this test if password has less than two digits than return false
    @Test
    fun `less than two digit password return false`() {
        val result = RegistrationUtil.validRegistrationInput(
            "Rahul",
            "abcd1",
            "abcd1"
        )
        assertThat(result).isFalse()
    }
}

步骤6:运行测试用例

要运行测试用例,请单击附近的小图标 类名,然后选择Run RegistrationUtilTest 。如果所有测试用例都通过,您将在“运行”控制台中看到一个绿色的勾号。在我们的案例中,所有测试均已通过。

Github仓库在这里

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处前往由我们的专家精心策划的指南,以使您立即做好行业准备!