Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

General Discussion

What is unit testing?

Can somebody tell me what is unit testing? I've heard of unit testing in Java, and saw unit testing in Swift? But what exactly is it, an what does it actually do?

2 Answers

Unit testing is a best practice when developing software. To break it down you write a bit of code and perform a test again't to see if it works as expected leaving less holes for bugs etc. A simple example would be

Creating a function that only returns an int from a string passed in javascript for example. That function isn't suppose to return no other value. So you code out the function a simple test would be having the return variable be tested to see if it is actually a an integer. After the test you will be sure that your code works as expected and code that utilizes this function will not break in let's say a calculation if a string was to be returned when the code was expecting an int it may cause your application to behave unexpectedly.

At least that's how I think about it. There are many practices I.E Test Driven Development

Can you give me a clearer explanation? I don't get what you mean by again't? Is it a file, or a piece of code, or a framework, or something? Is it with the program itself, or you have add it yourself? What is it used for?

It's code, your code here's an example

// return the number value of a string I.E "3" = 3;

function returnNumber(number){

    return number; // this should be parsed to an integer     

}

Say we use return number in a mathematical equation an sent in a letter instead of a number

var x = 3;

var y = returnNumber("a");

x * y = ?

This is where your code would break

A simple example to unit test the code would be to identify why the code break then solve it and make it unbreakable

example

function returnNumber(number){

 if(isNaN(number)){ // if the number sent in is not a number
           return 0; // just return 0
 }else{
          return number; // this should be parsed to an int
 }

}

Lets try our equation again

var x = 3;

var y = returnNumber("a");

// in this case why would be 0

x * y = ? // so 3 * 0 = 0

Notice our code didn't break even though we sent a letter instead of a number

var x = 3;

var y = returnNumber("3");

// y would be 3

x * y = ?// and 3 * 3 = 9

and out code works as intended too without breaking even if a number wasn't used

But what does "Include Unit Test" mean when create an Xcode project?

Simply means to set up your project to handle unit testing with an external library of some sort to make testing easier. If you don't plan on using it you can simple ignore it. But I recommend finding resources and learning how to do it, it helps you write better software.