OCUnit Framework

OCUnit Framework

tudip-logo

Tudip

24 June 2016

For writing the Unit Test cases in Xcode we have different framework by using which you can write test cases in objective C.


Please find some of these frameworks listed below:
  • OCUnit: A unit testing framework built into Xcode.
  • GHUnit: A third party unit testing framework with some extra features.
  • OCMock: This helps you in writing the mock objects to handle mocking in test cases.
  • XCTest: A unit testing framework that is introduced with Xcode 5.


We will start with OCUnit framework. If you have used any other unit testing frameworks in the past (like JUnit or NUnit) it is really easy to pick the concepts here.


To integrate OCUnit Framework please follow steps below:


Step 1: Create new project and select Single view Application
mkAJuyxbs6K2K9d7UFqoYzWXOFmldiygDCTLQnmYAPFTiZL6Squj_CrQk0nAJ5TYiHUfSr4jkJVICqvYROBVv_s_J5Q4IOnC7XOHhGZEjZRoxO61yoAtu-6-R9SK0qFlaKPoWWg

 

 

Step 2: Please select the “Include Unit Tests” to integrate the OCUnit framework in your Project.


nKxSbMClmQR-3CyI2tR-yVK6JnDgTn1CSUK_lk2QlRsgCjlW8_UOOjBm4dbjvBw2_8jihoDsfknX58RiRZa4-TZGXOm6BoESfcdyf8wzRi0yToo8bNihS2ETwuO99uINFVXx5_U


Step 3: Save the Project on a location of your choice in your machine


JbxZ0P8Oz61PdLS2R10a7UzM4boa5InlSilnDRi8I9Oa3-wRV5o-XmkEvN_fjiMJyOg1qb1rIsaWN9GEbHYz6RJT5xlWjRSdZGkeHgl5qXow8pOTcwsr7Gct3OuDlPbErKg7wpE

 

 

Step 4: After creating the project, it will highlight show the class file.


BqZhMaVeNr_LwYPAFChfAUibD0gwr9ChxoswOZxmiO_6qcnRJepog5ocrLaV3YXeqciERZV5EEu7-xNldBZvbCgvLwUxPQ9KHHX2XYZiYE5BRoitERZt8J4Yp6UOIzgs7rE2MJw


When you select the “Include Unit Tests” checkbox, Xcode automatically adds a unit test bundle target and a unit testing class to your project. Xcode also does most of the setup work for you. It configures the necessary build settings for unit testing and adds a target dependency in your application target builds, that would be invoked when you build the unit test target.


An OCUnit unit testing class contains two files: a header file with a .h extension and an implementation file with a .m extension. A newly created class’s header file looks similar to:


@interface xcodeDemoTests : SenTestCase
@end
The unit testing class, XcodeDemoTests in my example, inherits from the SenTestCase class, which is OCUnit’s base class.
Writing Test Methods
An OCUnit test method has very minimal set of requirements and they are mostly similar to any other unit test framework:
  • It must start with the word test. OCUnit treats methods with the test prefix as test methods and runs them automatically.
  • It does not take any argument.
  • It always returns void.
  • It supports generic assert framework, that is explained below.
OCUnit Assertions
Assertion macros are at the heart of the unit testing. They determine whether a specific test condition passes or not. OCUnit has the following assertion macros:
  1. STAssertEqualObjects: Asserts two Cocoa objects are equal.
  2. STAssertEqual: Asserts that two variables of a primitive data type, such as integers, are equal.
  3. STAssertEqualsWithAccuracy: Asserts that two floating-point variables are equal. This assertion allows you to deal with small inaccuracies in floating-point arithmetic.
  4. STAssertFalse: Asserts that given condition evaluates to false.
  5. STAssertFalseNoThrow: Asserts that given condition is false and no exceptions are thrown.
  6. STAssertNil: Asserts that a pointer is nil.
  7. STAssertNoThrow: Asserts that no exceptions are thrown.
  8. STAssertNoThrowSpecific: Asserts that no exceptions of a specific class are thrown.
  9. STAssertNoThrowSpecificNamed: Asserts that a specific exception is not thrown.
  10. STAssertNotNil: Asserts that a pointer is not nil.
  11. STAssertThrows: Asserts that an exception is thrown.
  12. STAssertThrowsSpecific: Asserts that an exception of a specific class is thrown.
  13. STAssertThrowsSpecificNamed: Asserts that a specific exception is thrown.
  14. STAssertTrue: Asserts that a condition evaluates to true.
  15. STAssertTrueNoThrow: Asserts that a condition evaluates to true and no exceptions are thrown.

setUp and tearDown Methods

  • setUp: If you have some initialization code block that you need to perform before running each test case, you can place that code in the setUp() method and remove the duplicate code in your tests.
  • tearDown: This method complements setUp method. If you want to run a code block that executes after every test case, that cleanup code should go to tearDown. A common case of using the tearDown method is to free any memory you allocated in the setUp() method.

search
Blog Categories
Request a quote