“prueba unitaria kotlin” Código de respuesta

prueba unitaria kotlin

Add dependencies
1. Open a Kotlin project in IntelliJ IDEA. If you don't already have a project, create one.
Specify JUnit 5 as your test framework when creating your project.
2. Open the build.gradle(.kts) file and add the following dependency to the Gradle configuration. This dependency will allow you to work with kotlin.test and JUnit:
dependencies {
    // Other dependencies.
    testImplementation(kotlin("test"))
}

3. Add the test task to the build.gradle(.kts) file:
tasks.test {
    useJUnitPlatform()
}



Add the code to test it
1. Open the main.kt file in src/main/kotlin.
The src directory contains Kotlin source files and resources. The main.kt file contains sample code that will print Hello, World!.
2. Create the Sample class with the sum() function that adds two integers together:

class Sample() {

    fun sum(a: Int, b: Int): Int {
        return a + b
    }
}




Create a test
1. In IntelliJ IDEA, select Code | Generate | Test... for the Sample class.
2. Specify the name of the test class. For example, SampleTest.
IntelliJ IDEA creates the SampleTest.kt file in the test directory. This directory contains Kotlin test source files and resources.
3. You can also manually create a *.kt file for tests in src/test/kotlin.
Add the test code for the sum() function in SampleTest.kt:
Check that the sum() function returns the expected value by using the assertEquals() function.
import kotlin.test.Test
import kotlin.test.assertEquals
internal class SampleTest {

    private val testSample: Sample = Sample()

    @Test
    fun testSum() {
        val expected = 42
        assertEquals(expected, testSample.sum(40, 2))
    }
}



Run a test
1. Run the test using the gutter icon.
2. Check the result in the Run tool window.
3. Make sure that the test works correctly by changing the expected variable value to 43:
@Test
fun testSum() {
    val expected = 43
    assertEquals(expected, classForTesting.sum(40, 2))
}
4. Run the test again and check the result.
android developer

Prueba de la unidad de Kotlin

/*
	Simple Junit4 Kotlin Test
*/
class SimpleTest {
	@Test
    fun test_1_plus_1_equal_2() {
    	val result = 1 + 1
        Assert.assertEquals(2, result)
    }
}
Eloge Junior FOSSO MBE

Respuestas similares a “prueba unitaria kotlin”

Preguntas similares a “prueba unitaria kotlin”

Más respuestas relacionadas con “prueba unitaria kotlin” en Kotlin

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código