The document discusses different types of software tests including unit tests, integration tests, and GUI tests. It notes that unit tests promote loose coupling between classes, add flexibility for future changes, and enable better code reuse. Mocks are mentioned as intercepting function calls to help with unit testing. The benefits of unit testing are listed as managing dependencies, speeding up tests, handling hard cases, and addressing incompleteness. The document recommends following a plan-modify-implement-assess cycle when making code changes and emphasizes the importance of reading the code.
2. - Pushes out timeline
- Boring
- When do you stop ?
- I hate it
- We have a QA department
- Broken window theory
- I hate it.
- We have GUI Tests we don’t need unit tests
- Perfectionism
7. You commit code without tests. You are a cowboy.
Cowboy no test
8.
9.
10.
11.
12. - Promotes loose coupling between classes
and sub systems
- Adds flexibility to code changes for future
changes
- Can enable better code reuse
- Easier to unit test
13.
14. Mocks act like a ping-pong paddle by
intercepting the function call and
immediately returning execution to the
calling function.
Broken window theory
Consider a building with a few broken windows. If the windows are not repaired, the tendency is for vandals to break a few more windows. Eventually, they may even break into the building, and if it's unoccupied, perhaps become squatters or light fires inside.
Introduced by James Q. Wilson and George L. Kelling
Cost of finding a bug.
Being clear …… Unit tests doesn’t take the place of other testing it just provides a more secure foundation
I hate working in the theoretical so lets pull a example out and work with it
Open up Thing.zip
The method Find should never return null
For some reason it is returning null find out why and fix it
And of course it needs to work just as before
Now before you get your hands on the code lets talk about some things we might do(Pulls open visual studio or just have them open the code no typing though)
What if we had unit Tests in this situation ? (Thing2.zip)
Simple Example but I still wouldn’t want to touch it (What if I break it)
Things to note:
Attribute above name of method
Name of method
Arrange – Setup everything
Act – Invoke the code to test
Assert – see if the code passes the criteria
Asserts can be tested serveral ways:
- Values returned by a method
- Values exposed through fields
- Values exposed through other methods or properties
- Values that come from outside the object, static state or a shared data structure
Ok that was a easy test ? But No return / Assert
Dependency Injection
- have a robot to build a house
- Teach it to build a wall
Once the wall is complete who builds the door ?
- The Robot knows how to build a wall and can grab a manufactured door and these doors can be changed out for different doors
Same Concept but with code
There is other types of injection as well but we aren’t going to talk about those right now
The differences is how Iadministrator is passed and used by the class
Dependency Injection in some ways allows us to solve for X with Mock Objects
There are events that can be fired if a method is called
you can make a mock return a particular value
-
Dependencies - Everything depends on everything else
Speed – faster the unit tests faster the more enjoyable this is going to be
Hard Cases – these are cases that are hard to reproduce
say you want to make sure to handle OutofMemoryException
Incompleteness – say you are working on a project with a team and you want to test just your part … this is a great way to do this
Creating the Mock Object
Mocking the method and saying it must be called
Mock.Assert on Administrator
Arrange – Setup everything needed for the running the tested code. This includes any initialization of dependencies, mocks and data needed for the test to run.
Act – Invoke the code under test
Assert – Specify the pass criteria for the test, which fails it if not met
Now we
What is a Test runner ? (Go Show THEM!)
JustTest (we own it)
MSTest (Free)
TestDriven.net(250 per seat)
Nunit (free)
- Cost might matter
- Nunit has more questions/answers on stackoverflow (Community)
- They all do the same thing … run Tests personal preference
(What is a mocking framework ?)
JustMock in my opinion wins hands down
- More documentation
- has a profilier (Mock Static Classes)
- We own it
- We can get support for it
Step 2 Demo
So …. How many of you have ever been to a lecture trying to sell you on 401K ? (raise your hand)
It has been said that selling TDD is a lot like selling someone 401K
convincing you that sacrificing now is going to be worth it later and asking you to take this, to some degree, on faith.
So there needs to be a benefit to convince people that there is in fact a better way
Learning how to use unit tests is going to take time … there is no way around it
This is a lot like the Tortoise in the hare
The Hare gets done quickly with mistakes (spends a lot more time troubleshooting)
The tortoise is slow because he is writing unit tests but when he is done …. It works and he knows why it works
But what if we could speed up the hare ?
How would could we do that ?
What if I told you I could write / change code and know instantly if it worked or failed ?
You don’t have to run the program …. Wait for it to spin up and click through screen after screen to get to what you want to test
All you have to do is type and you will be able to see if what you wrote worked or not
In Computer Science there is something that is often overlooked called a Feedback loop
At its core you do this when programming
- new feature
- identify new behaviors
- make changes
- run the application and test it
This is something called continuous testing
Scientific method
The question is how long does your feedback look take when developing and doing unit tests ?
In the older days of computers punch cards would take along time
When a feedback loop takes a lot of time you begin to wonder (lets go surf google anyone ?)
When we shorten the feedback loop people feel more productive
Which would you drather work on a application with slow feedback (Fadmin … cough) or one with a faster feedback (a new app
- So how can we make unit testing faster ? continuous testing
As soon after you write a line you will know if it failed or not
Ever tried to do code coverage and forget what you have tested and what you haven’t with unit tests ? (Not fool proof but better than guessing)
(Right click on the dot and it will tell you every tests that tests this line)
Is your unit test slow the yellow green will tell you
Hover over the dot and it will tell you how slow
Ever spend time trying to figure out why a unit test is failing and on what line ?
Run the Tests in the test runner …. Takes forever …. Where was I ?
Now you don’t run test runner … all your tests run in the background
Rarely build the application
Chess Game TDD Ncrunch - https://www.youtube.com/watch?v=1GngsnfKC3o