Unit tests in TDD part 1

Unit tests in TDD part 1

Unit tests

Unit tests are the quickest, easiest to write and cheapest to run. They generally test one outcome of one method at a time. They are independent of the Android framework.

The System Under Test (SUT) is one class and you focus only on it. All dependencies are considered to be working correctly — and ideally have their own unit tests — so They are mocked or stubbed.

This way, you have complete control of how the dependencies behave during the test.These tests are the fastest and least expensive tests you can write because they don’t require a device or emulator to run.

They are also called small tests. To give an example of an unit test, consider a game app.The Game class is one of the main classes.


A common use case is to increment the score using a function like incrementScore(). Whenever the score increments and exceeds the highscore, it should also increment the highscore. A simple and incomplete definition of the Game class can look like this:

Now go to write test this function:

If you run this test, you’ll see the test doesn’t pass. We now have our failing (red) test. You can then fix this to get our passing (green) test by writing the actual method for the Game class:

now if we can run test again we will find it is pass

Note

Google, in its testing fundamentals documentation, also suggests Robolectric for local unit tests. Robolectric simulates the Android runtime, it allows you to test code that depends on the framework without using a device or emulator. This means that these tests run fast because they run using just the regular JVM of your computer, just like any other test that uses JUnit and Mockito. However, some may consider Robolectric as an integration testing tool, because it helps you test integrating with the Android framework.

#InTheNextArtical we will write Unit Test for all case can you needed. in three sections


youssef badway

Senior Android Engineer @Clicks Egypt

1 年

final result ang go run your testCase will be pass

  • 该图片无替代文字
回复
youssef badway

Senior Android Engineer @Clicks Egypt

1 年

3

  • 该图片无替代文字
回复
youssef badway

Senior Android Engineer @Clicks Egypt

1 年

2

  • 该图片无替代文字
回复
youssef badway

Senior Android Engineer @Clicks Egypt

1 年

1

  • 该图片无替代文字
回复
Anas AbdElalieem ???

Information Security Engineer @Secure Networks | DFIR | OSINT | Gamer

1 年

???? ????? ?????? ?

要查看或添加评论,请登录

youssef badway的更多文章

  • Network security configuration

    Network security configuration

    The Network Security Configuration in Android is used to customize the behavior of your app's network stack. It allows…

    1 条评论
  • Writing Swift-Friendly Kotlin Multiplatform APIs — Part 1

    Writing Swift-Friendly Kotlin Multiplatform APIs — Part 1

    *This list contains some important notes and solutions for common issues in KMP projects So I will sum up these…

  • Unit Tests

    Unit Tests

    “The Testing Pyramid,” unit tests verify how isolated parts of your application work. Before checking how things work…

    12 条评论
  • Practicing Red-Green-Refactor

    Practicing Red-Green-Refactor

    In this article, you will learn the basics of the TDD process while walking through the Red-Green-Refactor steps…

    2 条评论
  • Compose and the View system can work together side by side.

    Compose and the View system can work together side by side.

    Introduction Compose and the View system can work together side by side. By the end of this article, you'll be able to…

    4 条评论
  • Understand Recomposition

    Understand Recomposition

    jetpack Compose is the newest thing in town. It is still in Alpha, but it shows promising signs.

    4 条评论
  • Dependency Injection pattern

    Dependency Injection pattern

    Most of the classes have some dependency that is needed for the proper functioning of the class. In the general case…

    5 条评论

社区洞察

其他会员也浏览了