DEV Community

My First Unit Test

Stojan Anastasov on September 26, 2017

There are different reasons developers write tests. Some of them are: Validate the system Feedback To prevent regression bugs Code coverage To en...
Collapse
 
daveblythe profile image
Dave Blythe (he/him)

Simply LOVE the "Geeks vs repetitive tasks" diagram :D

Collapse
 
s_anastasov profile image
Stojan Anastasov

There is also this xkcd comic. The awesome thing here is most of the automation code (the JUnit framework) was already written for me.

Collapse
 
daveblythe profile image
Dave Blythe (he/him)

Yep, the bulk of my unit testing experience was on PHPUnit :)

Collapse
 
andy profile image
Andy Zhao (he/him)

Great post! It feels great to automate something and save a ton of time doing it.

Test writing was a pain for me to begin with though, it really is like writing in another language. At least Ruby tests are like that anyway. What'd you think of the learning part of writing tests? Interested to hear your perspective on writing tests for Android/Java.

Collapse
 
s_anastasov profile image
Stojan Anastasov

When I first started developing, testing on Android sucked. The testing support was ActivityInstrumentationTestCase and ActivityInstrumentationTestCase2 both deprecated now - which means they fucked up twice :D. All test used to run on the emulator. On top of that I didn't have any experience with testing.

As I mentioned in my article since Android Studio 1.1 testing support was improved. We can run unit test on our computers if we don't have Android dependencies.

Of course to write unit test, first you need to write testable code. We have libraries for dependency injection like dagger and dagger2. We have the same tools as in java: junit and Mokito.

For UI tests we have Espresso. Android studio can record our interactions with the app and write tests code for us.

In the last Google I/O we got a few new Architecture components which help us build our app in a more modular and testable way.

As the testing support in the platform grow, my testing skills also get better and I write more tests. I still not at a place where I want to be, but I am getting there.

Collapse
 
ronalterde profile image
Steffen Ronalter

This is great. I have experienced similar things although working in a different field (Embedded software). You struggle with complex database setup and access. In Embedded it's the programming of the software into the target microcontroller and remote debugging that slows you down.

Collapse
 
brainwipe profile image
Rob Lang

I'm all for automation but you need to point out that the automation will cost something forever more. It might still be more efficient to automate but automation isn't without cost; it's not a nirvana.

Depending on your system, the benefit of automation is not always worth the cost. Highly coupled systems (e.g. UI to dB entities) are expensive to test anything beyond the simplest atomic level. Do try to get clearance from whoever is paying for your code because they may prefer the human method as the business value of test automation is low.

Collapse
 
rickab10 profile image
Stephany Henrique A

Very nice! To Start something new is always complicated, but with study and persistence you can reach.

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
s_anastasov profile image
Stojan Anastasov

I have no experience with angular. But if the functions are too big or do too much it would be difficult to test them. To make them more testable you could try splitting them.

Collapse
 
vasilvestre profile image
Valentin Silvestre

Time for some mutation testing .. :)

Collapse
 
saiyamchutani profile image
Saiyam chutani

also check out test Dpc and test Dpc apk

Collapse
 
2014abinash profile image
Abinash Mohanty • Edited

checkout Getapk Market

Collapse
 
dotsp0t profile image
dotSp0T

Obligatory xkcd: xkcd.com/1319/ and xkcd.com/1205/

Collapse
 
s_anastasov profile image
Stojan Anastasov

Check out my comment here