Why Do We Test Our Code? - Android Testing - Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys welcome back to new video and a new series which will be about testing in android so this first introduction video i'm going to talk about what are tests why should you write test cases and just the basic foundation you need to know to follow through this series well test cases in general they test our codes so they test if everything is working as it should that is pretty straightforward and you probably also tested your code in the past just by opening your app clicking around and seeing and verifying that everything works and i mean if something doesn't work then you usually detect that with clicking around but what is the problem with this approach because i used to do that for a long time too and the big problem is that it only detects errors once so whenever you want to check for errors then you need to open your app and click around which takes a huge amount of time and usually if something was working in your app then you just tested that once verified that it was working but then didn't touch it afterwards so what happens if you extend your app afterwards and then something breaks in the code that was previously working so let's be honest how often did you experience that because i did very often and that is the real problem of manual testing because you need to test the same functionality over and over again even if it was already working and that just costs us so much time and this is now where junit comes into play junit is a very popular library that is also um included in your dependencies by default in android studio and with the junit we can automate that testing process so we can explicitly write test cases that we just tell our program do this do that and afterwards this condition must be true or must we must be false so that now means if we do that then we can verify with a single click in a few milliseconds that the functionality of our app is working so even if we extend our app then we can just run our test cases that we have pre-written run them afterwards and then just check if everything is still working and even if this might sound cool but not too important right now trust me this is very important so if you listen to my professors in university a software is not complete without test cases and you will also not find a software without test cases a professional software at least in yeah the professional programming sector and in the end i do this series here because so many of you asked me to do a series about unit testing but i think many don't understand that well what unit tests actually are because unit tests are only one kind of test and i think what many of you really meant are just the playlists about testing because unit tests are yeah well they are super important but they are not the only tests we need in our app so let's actually better understand that if we take a look at this testing pyramid here here you can see there are unit tests integration tests and ui tests unit tests make up the base of that pyramid and unit tests are basically well you can think of it as single units they only test single components in our app and they are also the fastest tests in our program which probably run in few milliseconds in most cases and they are made to be run very often so a unit test could be for example that we have a function of that takes a list of integers and it calculates the sum of that list of integers and then we just give that function a list of integers that we created and we um calculate the sum out of that and then just make sure that the correct result comes out afterwards that would be a unit test because this wouldn't be dependent on any other component of our app it's just dependent on the single function and these unit tests should make up about 70 percent of the test cases of our app the next we have integration tests which test how different components of our app work together so for example how a fragment interacts with a viewmodel and if that probably works and here it's very important that you don't confuse integration tests with integrated tests because that is a big difference and both of these terms you will hear very often so integration tests are just as i said um they will test the interaction between different components in your app but integrated tests are tests that rely on android components and therefore must run on the emulator so if you take the example with the sum function again then this wouldn't be an integrated test because calculating a sum doesn't rely on any android components such as the context or an activity it just doesn't need that so it can run on the jvm directly and doesn't need an emulator to be run but if we for example have a function that needs to access our app's resources and for that we need the context then this would be an integrated test so that is something completely different than integration tests and it's very important to understand that difference so integration tests should make up about 20 of your test cases and then we have 10 percent missing for the ui tests all often also called end-to-end tests so in these types of tests they are really large and they they will basically guide the user through your whole app so they will test the whole interaction for example if you log in and then are navigated to the main fragment and from there you can add a to do item or whatever that would be a large test for example and for that we also use ui tests so ui tests basically verify that the ui has a specific state that we expect so for example if a checkbox is checked if a recycler view contains a specific item or if the textview has a specific text and since we test the ui here these tests must always run on the android emulator because it needs to show android ui of course so what will i actually cover in this series i will cover unit tests and integration tests because those big and large entry end tests would probably be too much for this playlist so we will also have some ui tests in this series but this will by no means be the focus of this playlist to write ui tests it's just that we need these ui tests for some of our integration tests so ui tests don't need to be on the top of our permit they can also occur on integration tests then the next video will be about a more theoretical topic as well and that will be how to write good test cases and test driven development just because it's so important so really don't skip these theoretical videos they are really important and then you're going to learn how to write your first unit test with junit so you can test your code with a single click you just learn the fundamentals of that we will start slowly so that you can keep up and we will work ourselves towards a wheel project which we'll also do in this series so we will work on a real mvvm android project with all that common stuff like navigation components view models um retrofit room and dagger so you really learn how you can apply test cases and the principles of test driven development in your real project because i'm not a fan of these theoretical videos that just show you a concept of testing in a program that is independent of a real project i think you really learn more if you learn how to apply these principles in a real app in an app that could be yours so i hope you are excited for the series if so tell me below and write me a comment philip i'm so excited for this series and yeah and if you're also not even a subscriber of my channel then quickly click on the button below subscribe to my channel and become one that would be a huge help for me and also for you because you get a video every second day about android development have an awesome day everyone see the next video bye bye [Music] you
Info
Channel: Philipp Lackner
Views: 35,377
Rating: undefined out of 5
Keywords: test, testing, android, test driven development, tdd, unit testing, integration test, fragment, mockito, junit, database, room, retrofit, dagger, hilt, integrated test, kotlin, android studio, jvm, coding, programming, development
Id: EkfVL5vCDmo
Channel Id: undefined
Length: 8min 56sec (536 seconds)
Published: Wed Aug 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.