an insight to test driven development and unit testing

16
Test Driven Development & Unit Testing - Dharmendra Prasad

Upload: dharmendra-prasad

Post on 19-Jan-2017

172 views

Category:

Software


0 download

TRANSCRIPT

Page 1: An insight to test driven development and unit testing

Test Driven Development & Unit Testing- Dharmendra Prasad

Page 2: An insight to test driven development and unit testing

◦ Is testing only a QA job?◦ Why should we bother?◦ What if I don't have tests?

Page 3: An insight to test driven development and unit testing

Testing is more of a Developer Job than QA It’s my responsibility towards others

Another developer can quick start Another developer will not spend time debugging your issues

Losing time on Debugging Production Issues No tests during development, will lead to more debug time on a

production bug day. You will not be there all the time.

Page 4: An insight to test driven development and unit testing

Test Driven Design Enforces Design for Testability

◦ Programming to Interfaces◦ Modularization◦ Avoiding Statics when possible

Test First Approach◦ May lead to minor changes in design

Finally TDD is a personal choice, no one can force you to use or not use it!!

Page 5: An insight to test driven development and unit testing

It puts you into the customer's shoe. It gives you an insight on completeness. Feature wise development. Identification of newer scenarios. It helps identifying & solidifying requirements. Once well written, it serves you for the life time of an

application.

Page 6: An insight to test driven development and unit testing

People are not much aware of its benefits Of course it is time consuming, both writing and reviewing

tests. Maintainability is an issue if not done properly

Page 7: An insight to test driven development and unit testing

Unit Test - A small piece of code to check another small piece of code.

Few criteria◦ Must be very Simple.◦ Must test only one thing◦ Must help in understanding the production code

Our Aim from Unit Testing◦ Make it Fail◦ Make it Work◦ Make it Better (better here means Simple)

Page 8: An insight to test driven development and unit testing

Write a Test Run a Test Review Results REPEAT

Page 9: An insight to test driven development and unit testing

Making Unit Tests Trustworthy ◦ Code coverage◦ Must test valid logic◦ Introduce bugs and re run◦ Start with Failing Test◦ Don’t use production code in test◦ Do not change production code if no failing tests◦ Never change or remove a test (Of course there are exceptions to

this rule)◦ Must Not contain logic◦ Know the difference between UT and IT

Page 10: An insight to test driven development and unit testing

Creating Maintainable Unit Test◦ Test only public methods◦ Enforce Test Isolation◦ Avoid Multiple Asserts in one test

Creating Maintainable Unit Test◦ Follow Naming Conventions Test_Scenario_ExpectedBehavior ◦ If testing with multiple parameters still use the same name with

number suffix

Page 11: An insight to test driven development and unit testing

Runs in memory Uses no external systems Should be fast Easily Repeatable Consistent irrespective of where they run. Should not be dependent on the third party services or

resources

Page 12: An insight to test driven development and unit testing

Write a Test◦ @Test methods, no arguments, no returns◦ Biggest impact is Naming Conventions◦ Unit Testing is about Arrange, Act and Assert

Arrange - create an instance of the class on which you want to act. Act - invoking the method which is to be tested Assert - most important part, validating the result

◦ Write tests for Valid data first

** If you do not assert it, then there is no point of this unit test. expected value to actual value. people tend to swap them which must be taken into account.

Page 13: An insight to test driven development and unit testing

Run a Test (Using JUnit)◦ Use Runners from the Frameworks

Runners are used for running test classes @Runwith specifies the runner - For e.g. @RunWith(SpringJUnit4ClassRunner.class) JUnit tests are started using the JUnitCore class (JUnitCore.runClasses(MyTestClass.class);)

◦ You can use Suite Runners Suite are collection of tests @Suite invokes the Suite Runner @SuiteClasses (to specify all the classes this suite will run)

Review Results◦ Verification and Improvements happen here.

Frameworks like Mockito provide MockitoJUnitRunner for automated mock initialization.

Page 14: An insight to test driven development and unit testing

What should be the first step after checking out the code? How many Unit Test Cases one should have?

◦ Consider a simple method with two arguments and a Boolean return

Page 15: An insight to test driven development and unit testing

Mock vs Stubs Subject of Assertion is called a Mock Object –

◦ We can have multiple stubs for a test. But may want to have just one mock object.

Everything else is a Stub. Why do we need Mocks?

◦ We cannot simulate all possible situations, downtimes, network error, Exceptions

Mock Frameworks are actually Isolation Frameworks. Mockito, JMock, EasyMock, MockServer (http://www.mock-server.com/)

Page 16: An insight to test driven development and unit testing

Thank You