Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do you test with Unit tests?

Tags:

unit-testing

I am new to unit testing. Suppose I am building a web application. How do I know what to test? All the examples that you see are some sort of basic sum function that really has no real value, or at least I've never written a function to add to inputs and then return!

So, my question...on a web application, what are the sort of things that need tested?

I know that this is a broad question but anything will be helpful. I would be interested in links or anything that gives real life examples as opposed to concept examples that don't have any real life usage.

like image 270
Icode4food Avatar asked Aug 23 '10 11:08

Icode4food


People also ask

What should I test with unit tests?

Unit tests should validate all of the details, the corner cases and boundary conditions, etc. Component, integration, UI, and functional tests should be used more sparingly, to validate the behavior of the APIs or application as a whole.

Where are unit tests used?

Unit Testing is done during the development (coding phase) of an application by the developers. Unit Tests isolate a section of code and verify its correctness. A unit may be an individual function, method, procedure, module, or object.


3 Answers

Take a look at your code, especially the bits where you have complex logic with loops, conditionals, etc, and ask yourself: How do I know if this works?

If you need to change the complex logic to take into account other corner cases then how do you know that the changes you introduce don't break the existing cases? This is precisely what unit testing is intended to address.

So, to answer your question about how it applies to web applications: suppose you have some code that lays out the page differently depending on the browser. One of your customers refuses to upgrade from IE6 and insists that you support that. So you unit test your layout code by simulating the connection string from IE6 and checking that the layout is what you expect.

A customer tells you they've found a security hole where using a particular cookie will give you administrator access. How do you know that you've fixed the bug and it doesn't happen again? Create a unit test for it, and run the unit tests on a daily basis so that you get an early warning if it fails.

You discover a bug where users with accents in their names get corrupted in the database. Abstract out the webform input from the database layer and add unit tests to ensure that (eg) UTF8 encoded data is stored in the database correctly and can be retrieved.

You get the idea. Anywhere where part of the process has a well-defined input and output is ideal for unit testing. Anything that doesn't is ideal for refactoring until it is well defined. Take a look at projects such as WebUnit, HTMLUnit, XMLUnit, CSSUnit.

like image 120
the_mandrill Avatar answered Oct 14 '22 00:10

the_mandrill


The first part of testing is to write testable applications. Separate out as much functionality as possible from the UI. Refactor into smaller methods. Learn about dependency injection, and try using that to create methods that can take simple, throw-away input that produces known (and therefor testable) results. Look at mocking tools.

Infrastructure and data layer code is easiest to test.

Look at behavior-driven testing as well as test-driven design. For my money, behavior testing is better than pure unit testing; you can follow use-cases, so that tests match against expected usage patterns.

like image 45
Cylon Cat Avatar answered Oct 14 '22 00:10

Cylon Cat


Unit testing means testing any unit of work, the smallest units of work are methods and functions., The art of unit testing is to define tests for a function that cannot just be checked by inspection, what unit test aims at is to test every possible functional requirement of a method.

Consider for example you have a login function, then there could be following tests that you could write for failures: 1. Does the function fail on empty username and password 2. Does the function fail on the correct username but the wrong password 3. Does the function fail on the correct password but the wrong username

The you would also write tests that the function would pass: 1. Does the function pass on correct username and password

This is just a basic example but this is what unit testing attempts to achieve, testing out things that may have been overlooked during development.

Then there is a purist approach too where a developer is first supposed to write tests and then the code to pass those tests (aka test driven development).

Resources: http://devzone.zend.com/article/2772 http://www.ibm.com/developerworks/library/j-mocktest.html

like image 33
ovais.tariq Avatar answered Oct 14 '22 00:10

ovais.tariq