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
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
34. Loosely-coupled and
Testable Code
• Extract the code that uses the external
resource into a separate class and isolate
it from a rest of you code
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