Join Ron Buencamino for an in-depth discussion in this video What are unit tests?, part of Automated Testing in Xcode.
- [Instructor] At a high level, unit testing refers to the practice of testing certain areas of function for a given software application. These areas of function are known as units and they can range in size. They can be small, little bits of code, or whole application modules. Now a test is code that you write, that exercises your apps code in a pass or fail result that is measured against a set of expectations. In Xcode, you can perform tests on your application code as well as dynamic libraries and frameworks.
So when it comes to testing, quite often we get posed a question, what exactly should we test? Well here are some common areas that you want to make sure you have tests for. First is your core functionality. You want to have tests that ensure primary functions of your application are performing as expected. Your core functionality is the main reason why people are using your app or service. So you need to make sure it's meeting your expectation. Next is common user interface workflow.
You want to test your user interface from a user's perspective to make sure that actual usage and workflow is as intended. You also want to test for boundary conditions, which are the extreme conditions that your app should meet to ensure satisfactory performance. And lastly, you want to write tests for bug fixes. Each time you fix a bug, it's smart to get into the habit of writing a test for it. You want to do this because doing so helps ensure that you're not fixing the same bug over and over again.
Now it's up for you to define what you want to test, but tests should cover the necessary components listed here to ensure that your app can run and scale in a coordinated manner. A helpful tip is to try dividing your code up as much as possible into smaller, testable components. Small, fast-running tests can be run often and can better diagnose problems in your code base. So what are some best practices that you can use for writing your tests? Well, you want to make sure you follow the acronym of F.I.R.S.T., F-I-R-S-T, when it comes to testing.
For F, we have Fast, as tests should run quickly, mainly so that people don't mind running them. For I, we have Independent, or sometimes called isolated, as you want to make sure your tests do not need to set up or tear down one another. Each test that you write should be independent and measurable on its own, without the reliance of another test. For R, we have Repeatable, as you should obtain the same result from your test every time you run it.
Now if you don't, chances are there's a problem with the bit of code that you're testing. For S, we have Self-validating, as tests should be fully automated with either a pass or a fail result. A person shouldn't have to interpret your log file. And for T, we have Timely, as tests should be written first, prior to writing actual code. But now that we have an idea of what unit testing is about, let's learn about how we can do this all in Xcode.
- What are unit and UI tests?
- Unit tests in Xcode
- Creating test targets
- Running tests
- Debugging tests
- Writing UI tests
- Code coverage