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

Web Application Testing

Web Application Testing

Introduction to software testing and types of testing (testing pyramid). TDD and Unit testing. Testing frameworks (JUnit, Spock) and Mocking/stubing (Mockito/EasyMock). Integration testing and API testing with Rest Assured.

Avatar for Tomche Delev

Tomche Delev

November 16, 2017
Tweet

More Decks by Tomche Delev

Other Decks in Programming

Transcript

  1. Agenda • Introduction to software testing • Types of testing

    (testing pyramid) • TDD • Unit testing ◦ Testing frameworks (JUnit, Spock) ◦ Mocking/stubing (Mockito/EasyMock) • Integration testing ◦ API testing with RestAssured
  2. Testing jargon • Unit testing • Service testing • Integration

    testing • Application testing • Acceptance testing • UI testing • End-to-end testing • Performance testing • Exploratory testing • Regression testing • Manual testing
  3. What is unit test? A unit test is a piece

    of a code (usually a method) that invokes another piece of code and checks the correctness of some assumptions afterward. If the assumptions turn out to be wrong, the unit test has failed. A "unit" is a method or function.
  4. But why? Ok let's stop (start) writing UT and see

    what happens. Wait... we've already tried do that, and we (don’t) know the result pretty well
  5. Many reasons why 1. Documentation 2. Refactoring 3. Rely on

    human factor 4. On failure the system is equally broken 5. Fear of reusing 6. Big changes more quickly 7. When to stop coding 8. The tests and the code work together to achieve better code (TDD). 9. TDD helps with coding constipation. 10. Unit Tests help you really understand the design of the code you are working on. 11. Instant visual feedback. 12. Unit testing does not mean writing twice as much code.
  6. Perfect unit tests? Fowler: "Imperfect tests, run frequently, are much

    better than perfect tests that are never written at all"
  7. What is not unit testing? UT cannot assert the correctness

    of your code. But it will constructively assert its incorrectness.
  8. How to do unit testing? • If it is hard

    to get under test -> The design of the code sucks. • If the code is hard to test -> It usually means we have not understand the code.
  9. 4 kinds of tests to write • Positive – Happy

    path • Negative – Unhappy path • Exception – The right kind of exception • Performance – If it’s important write a test for certain constants.
  10. Mocking out dependencies First, try to knock-out mocks and avoid

    them if possible. Don’t mock out the predicable dependencies (helpers, utils, etc.). When the dependency is slow and unpredictable, you may consider to mock it. Use mocking when its expensive to use the real object. Also use mocks to mock out web-services, file systems, databases and low level OS function (if possible). Mock objects are used to: • Simulate a behaviour • Simulate an ill behaviour
  11. Types of mocks • Dummy – Returns values as close

    to null or 0 as possible • Stub – Returns what it was told to return • Spy – The real object, but can see and count interactions and also stub out methods • Mock – A smart stub that you can verify calls and call order on • Fake – A little behaviour simulator for a certain test
  12. Hands-on tips • We don’t need to write tests for

    everything in the code, UI, setter/getter, etc. • UI may need test for critical things. • A ratio of 3-5 times more test code then production code. • Testing the UI begin at the model Level->Controller->View • The goal is not to write of lots of test, it’s to write meaningful test. Start with the behaviour/action/result. • If you putting for loops in your test, you writing the test wrong. Stop and rethink.
  13. Hands-on tips • A test should have no more than

    one independent assert (Single assert rule). Don’t do Act->Assert->Act->Assert. Independent assert = one block of asserts. • Memento pattern can be used to ease the testing by injecting the state you want to begin testing from. • Don’t be ceremonial over Given When Then and similar practices and processes. Use the most self explaining and minimalistic style you can come up with. Especially with naming, use short describing names.
  14. Spock Framework • Testing and specification framework for Java and

    Groovy applications • With beautiful and highly expressive specification language • Compatible with most IDEs, build tools, and continuous integration servers • Inspired from JUnit, jMock, RSpec, Groovy, Scala, Vulcans, and other fascinating life forms
  15. Spock in the testing jungle Use Case Conventional Tool(s) A

    Single Tool Unit Testing JUnit TestNG Spock Mocking and Stubing EasyMock jMock Mockito PowerMock jMockit Behaviour Driven Design (BDD) Cucamber JBehave
  16. Comparison to JUnit concepts JUnit Spock Test class Specification Test

    Feature Test method Feature method @Before setup() @After cleanup() Assertion Condition @Test(expected) Exception condition
  17. Hello Spock import spock.lang.Specification class StarshipSpec extends Specification { def

    "science officer of the starship Enterprise is Spock"() { given: def starship = new Starship("Enterprise", "Spock") when: def actual = starship.getScienceOfficer() then: actual == "Spock" } }
  18. Blocks State based testing (AAA) • Arrange • Act •

    Assert BDD Style • given • when • then
  19. Practices (1) • Start with a canary test • Don’t

    argue (with yourself or others) if a test is valid or not, just jot it down • Take small steps • Test behaviour, not state • A good design will need fewer tests • Think of positive, negative and exception tests • Write minimum of code to make the test pass • Code is fast to make the test pass
  20. Practices (2) • Not writing untested code • Gives focus

    on the interface that we’re evolving • Let the test fail first, then write minimum code to make the test pass • What if the test passes first? Ensure that tests passes for the right reasons • When write tests or writing code, you will think of more test conditions. Jot them down on your task list
  21. Principles (1) • YAGNI – You Aren’t Gonna Need It

    • Keep code and test DRY – Don’t Repeat Yourself • Ensure the test are isolated from each other • Test should be FAIR ◦ Fast ◦ Automated ◦ Isolated ◦ Repeatable
  22. Principles (2) • Keep test DRY an isolated set-up can

    help • Never refactor code when there is a red bar (failing tests) • Either refactor before the change or after the change • Refactor should take us from green to green. • Any time all test passes, it’s a great time for check in • Use SLAP – Single level of abstraction principle