Skip to content

Instantly share code, notes, and snippets.

View ArunYogeshwaran's full-sized avatar
🌎
Office

Arun ArunYogeshwaran

🌎
Office
View GitHub Profile
@ArunYogeshwaran
ArunYogeshwaran / AudioPlayer.kt
Created July 6, 2023 14:08
Behavior-driven tests can be easily understood by someone who's familiar with the system requirements. They also serve as indirect documentation of the code being tested, and they help in reducing the size of a test method, as only one behavior is tested at any time, even though the method under test does many things.
fun playArtist(artist: Artist) {
if (artist != Artist.TAYLOR_SWIFT) {
return IllegalArgException()
}
addToRecentlyPlayed(artist)
player.play(artist.getRandomSong())
}
@Test
fun `refer friend with new referral verify referral successful`() {
val friendReferral = getInstance()
friendReferral.referFriend("testUserId", getStaleReferral())
assertThat(referralList.contains("testUserId")).isTrue()
}
@Test
fun `refer friend with stale referral verify referral failue`() {
val friendReferral = getInstance()
// The condition here is that the private method is made public or package-private using the `VisibleForTesting` annotation.
@Test
fun `is referral valid with new referral expected value is true`() {
val friendReferral = getInstance()
asserTrue(friendReferral.isReferralValid(…))
}
// The condition here is that the private method is made public or package-private using the `VisibleForTesting` annotation.
@Test
fun `process referral valid with new referral verify referral successful`() {
@ArunYogeshwaran
ArunYogeshwaran / FriendReferral.kt
Created July 6, 2023 14:04
The example below shows a class that contains the logic of a feature in an app where a user can invite another user based on certain internal conditions.
fun referFriend(userId: String, referral: Referral) {
if (isReferralValid(referral)) {
processReferral(userId)
}
}
private fun isReferralValid(referral: Referral): Boolean {
  // Checks the validity of the referral.
}
@ArunYogeshwaran
ArunYogeshwaran / StateVsInteractionTesting.kt
Created July 6, 2023 14:02
The example below shows a system that toggles the "favorite" state of a given job in the cache.
// Unit under test.
fun toggleFavoriteForJob(job: Job) {
if (job.isFavorite) {
cache.unfavorite(job)
} else {
cache.favorite(job)
}
}
// A unit test using interaction testing.
// A test with relevant details abstracted out.
@Test
fun `get ML recommended jobs with new user expect predefined list`() {
// Test arrangements and setup.
val engine = new RecommendationEngine(getEnvDetails(), getLocalInfo(), getSessionProperties())
val jobs = engine.getJobs(getTestUserInfo())
assertThat(jobs).isEqualTo(expectedJobs)
}
// A test with relevant details in the test itself.
@Test
fun `get ML recommended jobs with new user expect predefined list`() {
  // Test arrangements and setup.
val engine = getRecommendationEngine()
val jobs = engine.getJobs(new User(UserStatus.FAIRLY_NEW, "testId", "Test User Name"))
assertThat(jobs).isEqualTo(expectedJobs) 
}
@ArunYogeshwaran
ArunYogeshwaran / DescriptiveTestNameExample.kt
Created July 6, 2023 13:55
The example below shows a simple system that fetches jobs for a user who can be either registered or unregistered.
// Unit under test.
fun fetchJobs(user: User) : List<Job> {
 if (user.isRegistered()) {
 return emptyList()
 } else {
 val jobs = jobsApi.getJobs(user)
 return jobs
 }
}
// A test with a non-descriptive name.
@ArunYogeshwaran
ArunYogeshwaran / AudioPlayer.kt
Created February 5, 2023 22:27
An example of testing methods vs behaviours
fun playArtist(artist: Artist) {
if (artist != Artist.TAYLOR_SWIFT) {
return IllegalArgException()
}
addToRecentlyPlayed(artist)
player.play(artist.getRandomSong())
}
@ArunYogeshwaran
ArunYogeshwaran / FriendReferral.kt
Created February 5, 2023 21:49
An example of using the public APIs to test
fun referFriend(userId: String, referral: Referral) {
if (isReferralValid(referral)) {
processReferral(userId)
}
}
private fun isReferralValid(referral: Referral): Boolean {
// Checks the validity of the referral
}