7. Benefits of Automated test
Why on earth should be write code to test
our code when we can just run the
application and see the result
8. Benefits of Automated test
โข Test your code frequently , in less time
โข Catch bugs before deploying
โข Deploy with confidence
โข Reduce the number of bugs that will go
into production
โข Refactor code with confidence
9. Benefits of Automated test
โข Refactoring means changing the structure
of the code with out changing its
behaviour
โข When you donโt have automated test
every time you refactor your code you
have to manually test every part of the
application
10.
11. Unit test
Tests a unit of an application without its external dependencies
Such as files , databases ,web services ..
โข Execute fast
โข Donโt give a lot of confidence about the application
12. Integration test
Test the application with its external dependencies
โข Take long to execute (read / write in DB)
โข Give us more confidence about the application
13. End to End test
Drives the application through its UI
There are a specific tools build for creating end to end test
Selenium
Allows us to record the interaction of the user with application
And check if the application return the right result or not ..
โข Give us a great confidence
โข Very slow
โข Small change can easily break this test
14. At the end of the day
Use your own judgment to determine what
kind of test you need
15.
16. Tooling
Nunit
earliest framework out there
MSTest
Microsoft testing framework built into visual studio
Xunit
gain more popularity over the past couple of years
17.
18.
19. Characteristics of Good
Unit Test
โข Clean , readable and maintainable
โข No Logic (if, loops,โฆ.etc)
โข Isolated
20. What to test and what not to
test
Unit testing and clean code go hand in hand
22. Attributes
โข Setup : call the method decorated with (SetUp attribute) before run each test
โข TearDown : call the method decorated with (TearDown attribute) after run
each test, often use with ingeneration test
โข TestCase : with this we can supply different argument for test method (this
attribute in NUnit only ) .
โข Ignore : ignore this test method .. And show us the reason why we ignore
this method . Better than deleting the method ..
24. TrustWorthy
Test you can relay on .. If the test passes you know that your code is working
And if it fails you know that is something goes wrong with your test
25.
26. How can we write TrustWorthy test?
There is to ways
โข Test driven development ..TDD
โข Code first
27. Test driven development
..TDD
Test Frist
with TDD you write your test before writing
production code
create a bug in your code if test pass that is
untrustworthy test
28.
29.
30. Unit Testing Techniques
โข Testing Strings
โข Testing Arrays and Collections
โข Testing the Return Type of Methods
โข Testing Void Methods
โข Testing Methods that Throw Exceptions
โข Testing Methods that Raise an Event
โข Testing Private Methods
31. If your code is not clean writing unit test is hard or
impossible
32. Loosely-coupled and
Testable Code
Most legacy applications are built without
unit testing .. So in order to unit test them
You need to refactor them
For testable and loosely-couple Design
33. Loosely-coupled and
Testable Code
it's a description of a relationship between two
classes, where each class knows the very least
about the other and each class could potentially
continue to work just fine whether the other is
present or not and without dependency on the
particular implementation of the other class.
There are 3 steps we need to follow to
achieve a testable and loosely-coupled
design
35. Next
Extract an interface from that class
Why an Interface ??
Because an interface like a contract .and
there is probably one or more classes that
implement this contract
36.
37. Execute
โข Refactoring Towards a Loosely-coupled Design
โข Dependency Injection via Method Parameters , Properties , Constructor
โข Creating Mock Objects Using Moq