What are tests and why should you write test cases ?
- To make it simple tests are just to check whether the code is working.
- But you can just test it simply then what's the deal with Test Driven Development ?? π€π€
- The issue here is to test a simple code you have to wait to open the App or run the code navigate to the.
- Well, you can do that for once but if you have several test cases where you have to test your code you'll waste a lot of time.
- Here comes TDD(Test Driven Development) in which we test a particular code with given test cases and check all of them once.
Understanding Tests in Android
- JUnit - JUnit is already included in our Gradle dependencies, using JUnit we can automate the testing process.
- Here we check the functionality of the app use it using different inputs of various test-cases and check them with the final result and the expected result.
- There are Threes kinds in testing -
- Unit Tests - These are the fastest tests in our program Used to test a single function of the app These make 70% of the testing in our app
- Integration Tests - To test how two components of our app work together i.e, to test the interaction between different components in the app. It is different from the integrated test(which require our app to run in the background) These make 20% of the testing in our app
- UI Tests - To test all the components of the app runs together These make 10% of the testing in our app
- Most of the testing part includes Units tests and Integration Tests
How to write good test cases and What is TDD ?
- TDD is a procedure of writing the test case before implementing the functionality
- To write good tests -
- Write the function of basic implementations or no content.
- Write the test cases which most probably fail the first implementation of the function.
- Now as our function does not pass all the test cases above we implement the logic until all the test cases are passed.
- In TDD we have a single assertion, now what is an assertion. Assertion is to make sure whether our test case pass or fails.
Let's start coding !!
To get started first I'm gonna start with a simple way to show the basics of unit testing, which can be applied in a real project
- by default in every project we have three dependencies as below
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
You may have noticed 'testImplementation' and 'androidTestImplementation' and also 'androidTest' and 'test' in the root package these are called source sets.
androidTest Now the Integrated Tests belong to the 'androidTest-Source set' which means they require an android emulator to run the tests. and to add dependencies to this source set we use 'androidTestImplementation' in build.gradle file.
test And the tests that don't require any android components such as context. these tests run on JVM and run fast. and to add dependencies to this source set we use 'testImplementation' in build.gradle file.
Enough with the Theory now
There is a CodeForces problem which, we are going to solve using TDD
A. MagnetsIn this problem, we have to find out how many groups of magnets can be found if they are arranged in a horizontal manner.
Let's create a function that takes an integer and a string as an input and gives no of groups i.e, integer as an output. Now right-click on Magnets and then Generate>Test
Select JUnit4 as the Testing Library and press OK then select test source set as below
Add the below dependency and after sync, create the code forces test cases as below and run Magnetstest which was created in the above step.
testImplementation 'com.google.truth:truth:1.0.1'
// to make assertions easy and readable
As our result is 0 always and it doesn't match with the test cases we fail both the test cases.
Now after implementing the correct implementation of the function as below we pass both the test cases.
object Magnets {
fun noOfGroupsOfMagnets(n : Int, magnets: Array<String>) : Int{
var result = 0
for(i in 0..n-2) if (magnets[i]!=magnets[i+1]) result++
return ++result
// ~~ simple solution ~~
}
}
Do follow me on DEV.to
For more information on TDD, checkout
Testing-on-Android/Philipp Lackner
Top comments (0)