Skip to content

Instantly share code, notes, and snippets.

@uhooi
Last active May 18, 2020 01:13
Show Gist options
  • Save uhooi/a8edcf5182437874626543794a82b8f2 to your computer and use it in GitHub Desktop.
Save uhooi/a8edcf5182437874626543794a82b8f2 to your computer and use it in GitHub Desktop.
Unit testing sample: 2. Test Method
package packagename
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
class ExamLogicTest {
// region Stored Instance Properties
private var examLogic = ExamLogic()
// endregion
// region TestCase Life-Cycle Methods
@Before
fun setUp() {
reset()
}
@After
fun tearDown() {
}
// endregion
// region Test Methods
// region scoreExam(point:)
@Test
fun scoreExam() {
val testCases = listOf(
Pair(-1, "変な値を送るな!"),
Pair(0, "赤点です!"),
Pair(29, "赤点です!"),
Pair(30, "まあまあです!"),
Pair(79, "まあまあです!"),
Pair(80, "やるじゃん!"),
Pair(99, "やるじゃん!"),
Pair(100, "天才です!"),
Pair(101, "変な値を送るな!")
)
for ((point, expected) in testCases) {
reset()
val actual = this.examLogic.scoreExam(point)
assertEquals(actual, expected)
}
}
// endregion
// endregion
// region Other Private Methods
private fun reset() {
this.examLogic = ExamLogic()
}
// endregion
}
import XCTest
@testable import TargetName
final class ExamLogicTests: XCTestCase {
// MARK: Stored Instance Properties
private var examLogic = ExamLogic()
// MARK: TestCase Life-Cycle Methods
override func setUp() {
reset()
}
override func tearDown() {
}
// MARK: - Test Methods
// MARK: scoreExam(point:)
func test_scoreExam() {
typealias TestCase = (point: Int, expected: String, line: UInt)
let testCases: [TestCase] = [
(-1, "変な値を送るな!", #line),
(0, "赤点です!", #line),
(29, "赤点です!", #line),
(30, "まあまあです!", #line),
(79, "まあまあです!", #line),
(80, "やるじゃん!", #line),
(99, "やるじゃん!", #line),
(100, "天才です!", #line),
(101, "変な値を送るな!", #line)
]
for (point, expected, line) in testCases {
reset()
let actual = self.examLogic.scoreExam(point: point)
XCTAssertEqual(actual, expected, line: line)
}
}
// MARK: - Other Private Methods
private func reset() {
self.examLogic = ExamLogic()
}
}
@uhooi
Copy link
Author

uhooi commented May 18, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment