Last active
October 9, 2020 18:55
-
-
Save alwarren/b2fc00749963bd12ee7f863a45e2569c to your computer and use it in GitHub Desktop.
Common Unit Test Structure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class AbstractUnitTest { | |
private var counter: Int = 0 | |
private var passed: Int = 0 | |
@BeforeAll | |
abstract fun beforeAll() | |
abstract fun actualMethods(): List<String> | |
abstract fun expectedMethods(): List<String> | |
abstract fun actualProperties(): List<String> | |
abstract fun expectedProperties(): List<String> | |
@AfterEach | |
fun afterEach() { | |
++counter | |
} | |
@AfterAll | |
fun afterAll() { | |
println("End of Test Suite") | |
println(String.format("Passed %s of %s tests\n", passed, counter)) | |
} | |
protected fun showPassed() { | |
passed++ | |
println(Values.PASSED) | |
} | |
protected fun noTests() { | |
print(String.format(Values.TEST_TITLE_FORMAT, Values.NO_TESTS)) | |
counter-- | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | |
class BaseUnitTest : AbstractUnitTest() { | |
@BeforeAll | |
override fun beforeAll() { | |
println(Values.suiteTitle("UnitTestStub")) | |
} | |
@Nested | |
@DisplayName("API") | |
@TestMethodOrder(MethodOrderer.OrderAnnotation::class) | |
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | |
inner class Api { | |
@BeforeAll | |
fun setup() { | |
println(Values.classTitle("API")) | |
} | |
@Order(1) | |
@Test | |
fun `verify expected properties`() { | |
print(Values.testTitle("Verify Expected Properties")) | |
actualProperties() shouldEqual expectedProperties() | |
showPassed() | |
} | |
@Order(2) | |
@Test | |
fun `verify expected methods`() { | |
print(Values.testTitle("Verify Expected Methods")) | |
actualMethods() shouldEqual expectedMethods() | |
showPassed() | |
} | |
} | |
@Nested | |
@DisplayName("Behavior") | |
@TestMethodOrder(MethodOrderer.OrderAnnotation::class) | |
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | |
inner class Behavior { | |
@BeforeAll | |
fun setup() { | |
println(Values.classTitle("Behavior")) | |
} | |
@Test | |
@Order(1) | |
fun `no tests`() { | |
noTests() | |
} | |
} | |
@Nested | |
@DisplayName("Corner Cases") | |
@TestMethodOrder(MethodOrderer.OrderAnnotation::class) | |
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | |
inner class CornerCases { | |
@BeforeAll | |
fun setup() { | |
println(Values.classTitle("Corner Cases")) | |
} | |
@Test | |
@Order(1) | |
fun `no tests`() { | |
noTests() | |
} | |
} | |
override fun actualMethods(): List<String> = | |
emptyList() | |
override fun expectedMethods(): List<String> = | |
emptyList() | |
override fun actualProperties(): List<String> = | |
emptyList() | |
override fun expectedProperties(): List<String> = | |
emptyList() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MetarIngest Test Suite 2020-10-09 12:41 PM | |
API | |
Verify Expected Properties => PASSED | |
Verify Expected Methods => PASSED | |
Behavior | |
No Tests | |
Corner Cases | |
No Tests | |
End of Test Suite | |
Passed 2 of 2 tests | |
MetarFile Test Suite 2020-10-09 12:41 PM | |
API | |
Verify Expected Properties => PASSED | |
Verify Expected Methods => PASSED | |
Behavior | |
Verify getStation result => PASSED | |
Verify find result => PASSED | |
Corner Cases | |
No Tests | |
End of Test Suite | |
Passed 4 of 4 tests |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Values { | |
const val PASSED = "PASSED" | |
const val TEST_TITLE_FORMAT = " %s\n" | |
const val NO_TESTS = "No Tests" | |
private const val TEST_CLASS_FORMAT = " %s" | |
private const val TEST_FORMAT = " %s => " | |
private fun dateTime(): String { | |
val pattern = "yyyy-MM-dd h:mm a" | |
val simpleDateFormat = SimpleDateFormat(pattern) | |
return simpleDateFormat.format(Date()) | |
} | |
fun suiteTitle(title: String): String = | |
String.format("\n%s Test Suite " + dateTime() + "\n", title) | |
fun classTitle(title: String): String = | |
String.format(TEST_CLASS_FORMAT, title) | |
fun testTitle(title: String): String = | |
String.format(TEST_FORMAT, title) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment