Testing Rebooted (with AndroidX Test) (Android Dev Summit ’18)

[Music] it’s high-quality to look so many men and women here eager about trying out my name is jonathan garish and along with my colleague Yuki Hamada we’re going to be presenting a session on trying out api’s at present so simply to get started simply hands up anybody who’s written unit assessments it is no longer a trick question excellent and what about integration assessments you can be sincere super good enough let’s get began so on Android there may be two sorts of tests you might be accustomed to there may be local unit exams after which there is instrumentation exams so let’s by using looking at local unit assessments so these are ones that these are checks which can be achieved in your neighborhood developers pc on the local VM going for walks there and for the reason that they you do not have got to run the entire Android construct chain your void outside and installing on a gadget these assessments are clearly fairly relatively rapid on these kind of exams you should utilize a software like robolectric which comes with its own set of trying out api’s for establishing the state of your android atmosphere or you need to use a tool like Marketo and you can stub out the interactions with the android framework and either approach they allow you to put in writing tests developing the state of the environment that satisfy the preconditions that you just could need in your test case the second kind of assessments are instrumentation checks now these are the assessments so that it will run on a virtual or a real gadget a real gadget might simply be a mobilephone linked to your laptop or it would be a farm of devices someplace within the cloud these sort of checks run slower on account that you need to execute the entire build chain and install an application onto the gadget but they have got the skills of being a lot more accurate due to the fact that the real or virtual gadget is an extraordinarily equivalent some instances equal to instruments that your customers might be utilizing out in the area and this brings you the arrogance that your app goes to behave as you count on one criticism we’ve got heard is that these sort of exams there’s definitely a scarcity of trying out API is available which makes it complex so that you can mounted the state of your environment in a technique that satisfies designated preconditions or edge instances that you just could need to be testing and so we have heard you loud and clear and this is anything we’re actively engaged on it is a bit of somewhat historical past lesson on 2017 at Google i/o we provided what we name the Android testing story say based loosely round just like the program progress pyramid and in this model we inspire you to put in writing tons and tons of fast scalable unit exams that scan all of your exhaustive stipulations we motivate you to write a smaller quantity of instrumentation exams on the way to truely prove that all these models collect collectively behave as you can expect on an actual gadget and in some ways this used to be style of a compromise it was once a compromise between the advantages of one set of 1 type of test and the trade-offs of one more so it brings you a like a holistic approach of checking out your app and we showed how this sort of method can result in experiment rim development on Android to start with you would with a failing UI experiment this may be an instrumentation challenge ordinarily written with espresso and it might experiment the UI of your of your factor your characteristic and then you can fulfill that function by using a sequence of items lessons coming together their interactions and also you could experiment pressure these as well making use of a instrument like robolectric or mojito running on as a nearby experiment and this gives you very quick progress cycles eventually whilst you convey all of them collectively you are able to run slower jogging but more trustworthy instrumentation experiment and confidently it goes green and you’re executed well we enter a refactoring cycle due to the fact that perhaps you your code leaves a bit of to be favored and you need to do some cleanup and so you could spend some refactoring cycles there earlier than coming round to the opening of the cycle where when you have any further work on the function you might add another test experiment a further side to that feature and you can hold iterating except you entire at which time you’re excellent to submit your code and transfer on to the next undertaking so at Google i/o this year we realized there was once a somewhat of a test writing crisis and seeing that there may be so many tools available it can be not normally clear which one to use and every of these tools all have their own unique patterns and api’s and paradigms for these identical standards that exist on Android and the difficulty with this is that assessments written in special levels are usually not transportable throughout stages your test is type of caught it’s coupled to the checking out device and environment that you’ve got written it on so this 12 months at Google i/o we introduced beta of Android X assessments it brings checking out as a satisfactory citizen as part of the tool chain as part of jetpack did we comprise some of the present libraries you have used earlier than some new API is full Kotlin help which allows you to jot down really wonderful and concise tests and it is on hand on and off device well last week Android X checks moved out of beta and tour the first full and ultimate release 1.Zero it is usually as of last week absolutely open sourced and so we seem ahead to welcoming your contributions and the entire documentation on developer.Android.Com cut down training shrink trying out there’s all been revamped to show you the new patterns of api’s so please do not determine that out so let’s take a appear inside the first module that we pulled throughout used to be the present je unit for aid and the runner and the roles that you may have used before we have also introduced a new module which we name core and this includes some new api’s application supplier as its title suggests it’s a rapid and effortless approach to get a keep of the applying context exercise situation which is a manufacturer new API that provides coarse and excellent grained api’s for which that you can test your pursuits and fragment situation which was once launched really simply this week delivering this very identical set of testing points for fragments we now have additionally brought espresso into the jetpack Android X family espresso if you are now not mindful is a set of view matching library stuffed with a set of view matching api’s and a suite of view movements it can be permit you to match after which in have interaction with these UI elements it also entails every other things like capability to seize and stub extreme for the procedure and eventually we now have additionally launched some truth Android extensions and truth is Google’s open supply fluent testing assertions library and we introduced in a bunch of accessories for Android subjects which allow you to test your Android objects in a method that reads superbly and concisely and those have you ever been utilizing robolectric we are going to comprehend that we have now had a variation four.Zero and beta for a whilst and as of last week we did a simultaneous free up that is now long past into final robolectric four totally supports all the unified API is that are in Android X scan as good as a quantity of its new elements and enhancements k and so i’d like to welcome you key onstage who’s gonna provide you with a deeper dive into some of the api’s available yuki $6 sound hello everyone so let me introduce our new api’s let’s with application supplier which is a new approach of having access to context from your test board so whilst you work on Android testing you have got to manage two one of a kind contexts object the primary one is come from come from utility sorry the primary one is come from application below your scan and the second one is come from the instrumentation apk the place your test code is saved so with modern a latest library we have two exclusive approaches to entry this context object and this makes your scan code tougher to comprehend because the library makes use of library designated terminology goal context approach context from your instrumentation apk even as sorry the target context approach the context out of your application while get get context means the context out of your instrumentation apk also it’s it’s not without doubt which one to make use of in your test so in our new API we conceal the instrumentation content material from public API and we only and simplest application supplier best presents software context within the kind over your application graph let’s take a appear on the illustration code so here let’s assume we now have a experiment for the region tracker pastime and within the setup approach we get that we get the target context within the historical-common approach and we variety simply it to a vicinity tracker software in order that we will register a mock mock object for checking out and 2nd line set the mock object for that and this code is lovely easy and it clearly works however you could have used our possess context and face-to-face terrain time at runtime error and ended up with losing your time for debugging now with the new method a prior application context supplier presents your context in a to your application type so you are able to do the exactly the equal thing however there is so much less risk for our confusion or baggage k pay attention let me transfer on to the extra tricky stuff recreation state of affairs certainly before I we dive into important points i’ve a query for you how many of you have got written your possess endeavor and and manage the lifecycle transitions by you up by your self please elevate your hand if you are cool and how many of you have got shipped to your undertaking with with the our bodies related to existence cycles Oh a lot of you and who failed to like a style for that as we did not write a experiment cool good yeah I I see some one hundred maintaining up and to be honest I do too and i agree the writing scan for the pastime exercise lifecycle transition is pretty rough and there is not any this kind of excellent API in checking out library as now so that’s why me and the Jonathan and our workforce have searched for an answer and we boost the endeavor scenario which you should use it for driving your activity stay tuned arbitrary state for testing so let’s revisit the activities undertaking state first so the creative state is where an recreation is created I mean instantiated however it’s but it’s not noticeable to customers yet or activity possibility so it can be inventive state even as it is strolling in background as well and the gain knowledge of state is we are endeavor is created in be taught it’s partially noticeable to customers however no longer the Frog on foreground pastime routine going for walks in picture-in-photo mode can also be on this state and an inexpensive state if we are your recreation is utterly visible to customers and going for walks in foreground and in and underneath the framework can alternate the lifecycle state by each time via the user person interactions so your endeavor has to control these state transition property for a good person experience otherwise you see some bugs and pastime scenario presents a approach movie to state which which you can power your activities like circus like cycle stay tuned arbitrary state for trying out let’s take a seem at an instance referred to as here we have now a scan for a vicinity tracker activity and right here we need to verify that vicinity listener is effectively unregistered from the approach when the endeavor becomes ingenious state so in the beginning we have got to exercise launch exercise takes your undertaking your exercise category and start activity and wait look ahead to it unless it becomes resuming the state and transfer it to state initiated lifecycle transition and movie megastar lifecycle state to the inventive state also the older procedure the entire hobbies module procedure works as a blocking name so after this after this methodical it is assured that the events lifecycle state to be created and then you could inspect your pursuits internal state by calling possess recreation approach sure that is effortless now we’ve got a API and likewise you should utilize activity scenario for testing the creation of your endeavor so recreation activity occurs if you end up your pastime is going for walks in history for a long time and while you come again it later and your endeavor has to save its its interior state to establish saved inner shop this stable illustration a bundle earlier than it’s destroyed otherwise you’re going to lose the state and undertaking learn has a method recreate we’re you can use it for trying out this sort of situation this see our instance code so here on this experiment we need to ensure that input textual content is restore the property after the pastime is recreated after destruction so first we’ve a some test knowledge like test consumer as an enter and once more we pastime and then we enter we fill the textual content box by making use of the espresso library as you’ll find the pastime situation works beautiful effectively with espresso after which we call recreate which sincerely leak with which in reality destroyed reside recreation and recreate the brand new example and waits waits for the exercise to be resumed in state and on the other hand making use of the espresso once more and we can ensure that the textual content is there i will be anticipated sure it’s also simple adequate finally i would like to show you one more example from the truth teeth extension in 10 topics by way of use of severe area that you could affirm your you could verify your intent values and and it produces relatively just right human-friendly error messages if the error happens let’s have a look at an illustration referred to as in this time we wish to again ensure that intent we want to be certain our data intent has a expected contact name in it extras so first we create a bundle of the info intent and then we assert through three strains one is checking if that intent has anticipated motion and the second is variety and the 1/3 one is the additional bundles and if the worth doesn’t fit your expectation you see this error as you will find like that you may immediately recognize the on this example the intent action shouldn’t be isn’t what you expected and yellow tons of a factor come with underneath the x1 under the XS 1.Zero and that i can’t exhibit you everything at present well for illustration like we have more assertions and we even have under the builders we’re you can create your checking out data without difficulty and also scenarios for endeavor and fragments and you could take a seem at this hyperlink or documentation to look more i am hoping you to take a look at it out after the speak ok so that is the our solution for the test writing main issue so with unified API you not have got to you no longer don’t forget video to write down instrumentation test or the robolectric scan due to the fact that that you would be able to now simply write a beneath your test and that test runs on each each grunt both runtime environments nicely so with the unified API that you could focus on what to scan and that you could forget about where and the way and to make sure the consistency of the behavior of our API we now have a verification test and we run the identical experiment in opposition to sorry we run the equal experiment on in the community with robolectric and likewise we run our exams on virtual instruments from the AP from the API level 15 to the today’s variant and and this malicious program return to the workflow we confirmed you previous in this talk which will execute the experiment experiment-pushed progress way more effectually utilizing the device agnostic test with the Dostal experiment writing in a unified API as our suggestion we propose that you just run the you write the experiment and a Rando experiment with the lab electric after which unless your code is ready to publish and after which run doubt run the equal test however on the virtual instruments before surely you put up a scan to maximise your self assurance also that you could run the equal scan as a continuous integration scan against the nightly constructed binary with the upcoming nitrogen to change that you would be able to installed one of these configuration comfortably in case you if you want to comprehend more about the venture nitrogen we’ve got a session the next day to come so and we incredibly endorse attending it Kurt I thanks very a lot for listening and have a havi testing [Applause] [Music]

Add Comment