Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Effective testing strategies to raise code quality

Eeb061c8b2816b771920da1b3e7904a3?s=47 Swift India
February 09, 2019

Effective testing strategies to raise code quality

Eeb061c8b2816b771920da1b3e7904a3?s=128

Swift India

February 09, 2019
Tweet

Transcript

  1. Test Doubles & Test Data (First class citizens for writing

    unit test) By MURALIDHARAN KATHIRESAN
  2. Quality Agility Early bug discovery Better Understanding Courage to refactor

    And improved productivity Why to Test the code?
  3. What makes code hard to test ? Let’s review some

    anti-patterns, code smells, and bad practices that we should avoid when writing testable code
  4. None
  5. What makes a Good Unit Test ? Perform the bare

    minimum of work required to achieve its goal Be easy to understand Be easy to maintain Execute in a shorter period of time A descriptive test name Narrow and Focused
  6. Unit Testing Best Practices examples

  7. Continued.. One assert per test method Avoid test interdependence Keep

    it short, sweet and visible Recognize test setup pain as a smell Add unit tests to Continuous integration build
  8. What we follow in News? JSON Parsing Tests Snapshot Tests

    ViewState Tests Presenter Tests UseCase Tests
  9. A test double is an object that can stand in

    for a real object in a test, similar to how a stunt double stands in for an actor in a movie. These are sometimes all commonly referred to as “mocks”, but it's important to distinguish between the different types of test doubles since they all have different uses. Test Doubles
  10. Dummy A dummy implementation is an object which is not

    used in our test. Basically, we merely use it to let compile the test.
  11. Stub A Stub is a dummy that returns a specific

    value because the rest of the system relying on this specific value to continue running in a test. A stub has no logic, and only returns what we tell it to return
  12. Spy A Spy dependency is a more powerful version of

    the Stub one. It provides information based on how its methods are called—like the parameters values and the exact order methods are called
  13. Mock A mock is similar to a spy and does

    a little more. It also tests behavior by having the assertion go into the mock itself
  14. Test Input sources sources Local JSON Test Data Local object

    instances
  15. It can be called like: User.testData(userName: “Murali”) Test Data

  16. testData() is a static function that create an object Only

    one testData() for type testData() can create an object passing no arguments as all its property have default values default values reuse existing testData() whatever is possible default values use nil when the property is optional Benefits of Test Data?
  17. Credits Special thanks to ○ Aaina vivek jain ○ Giuseppe

    basile who introduced me the concept of test doubles and helped me to write quality code!
  18. Thanks! Any questions? You can find me at ○ @https://twitter.com/muralidharan_k

  19. https://medium.com/@gbasile/efficient-test-data-creation-in-swift-822fc14fef27 https://martinfowler.com/bliki/TestDouble.html https://www.toptal.com/qa/how-to-write-testable-code-and-why-it-matters https://clean-swift.com/swifty-little-mocker/ References