我是单元测试的新手。我已经承担了测试此代码的任务。我了解我必须使用assertEquals来检查是否 RegionData.Key.DEV返回VZCRegion.Development。 任何帮助,将不胜感激。
fun fromCakeSliceRegion(cakeSliceIndex: RegionData.Key): VZCRegion {
return when (cakeSliceIndex) {
RegionData.Key.DEV -> VZCRegion.Development
RegionData.Key.EU_TEST -> VZCRegion.EuropeTest
RegionData.Key.US_TEST -> VZCRegion.UnitedStatesTest
RegionData.Key.US_STAGING -> VZCRegion.UnitedStatesStage
RegionData.Key.EU_STAGING -> VZCRegion.EuropeStage
RegionData.Key.LOCAL, RegionData.Key.EU_LIVE -> VZCRegion.Europe
RegionData.Key.AP_LIVE, RegionData.Key.US_LIVE -> VZCRegion.UnitedStates
RegionData.Key.PERFORMANCE, RegionData.Key.PERFORMANCE -> VZCRegion.Performance
}
答案 0 :(得分:0)
通常,Kotlin中的Testclass如下:
import org.junit.Assert.assertTrue
class NodeTest {
@Test
fun neighbourCountValidation(){
//This is a snipped of my test class, apply your tests here.
val testNode = Node(Point(2,0))
assertTrue(testNode.neighbourCount()==0)
}
}
对于您要测试的每个类,创建另一个测试类。现在,对于每个用例,创建一个将测试此行为的方法。就我而言,我想测试一个新的节点是否没有邻居。
确保在build.gradle中实现junit环境
希望您可以将此构造应用于您的问题
答案 1 :(得分:0)
首先欢迎您使用stackoverflow!
要开始进行单元测试,我建议您总体上阅读它们,这是一个很好的起点another stackoverflow answer
现在返回您的测试。您应该在 test目录下创建测试类,而不是在主软件包中。
该类可能看起来像
import org.junit.After
import org.junit.Assert
import org.junit.Before
import org.junit.Test
class TestCakeSlice {
@Before
fun setUp() {
// this will run before every test
// usually used for common setup between tests
}
@After
fun tearDown() {
// this will run after every test
// usually reset states, and cleanup
}
@Test
fun testSlideDev_returnsDevelopment() {
val result = fromCakeSliceRegion(RegionData.Key.DEV)
Assert.assertEquals(result, VZCRegion.Development)
}
@Test
fun `fun fact you can write your unit tests like this which is easier to read`() {
val result = fromCakeSliceRegion(RegionData.Key.DEV)
Assert.assertEquals(result, VZCRegion.Development)
}
}