Search code examples
objective-cunit-testingsingletonocunit

Ocunit - Creating singleton test class to share code among classes


I just started to write unit test case for my code. While writing I just absorbed that many test case classes are using some common code and also it need a common variable in among the classes.

So I have decided to create a singleton class to hold all these values. In which this singleton class will be inherited with sentesting.

So Here my question is it possible to create a singleton test class. ?

Note : I have created a singleton but it is not at all working.


Solution

  • OCUnit shares the same architecture as the other xUnit frameworks. To run a test case, it

    1. Instantiates a test case object. (It's usually called a "test fixture" where the fixture has ivars of other objects useful across several tests.)
    2. Calls setUp
    3. Runs one specific test method
    4. Calls tearDown

    For the common variable you want, define it as an ivar. Then initialize it in -setUp. If it needs explicit cleanup afterwards, do so in -tearDown.

    It's very important to avoid sharing objects between tests, because each test should stand have consistent pass/fail behavior on its own. Sharing objects pollutes this, creating potential dependencies between your tests. Since singletons are a common way to share objects, they generally work against unit testing strategy.

    For a step-by-step example of unit testing (specifically test-driven development) in Xcode using Objective-C or Swift, see https://qualitycoding.org/tdd-kata/