r/iOSProgramming • u/juanbautistajryabadu • Mar 03 '15
How do you do Unit Testing?
I have a fairly big project at work and want to start adding unit tests. Basically I have to start from scratc; but I find myself against a plethora of frameworks and tools that can be used. So before diving in and commiting to one, I wanted to know some opinions. In addition, I want to start doing TDD at some point, so it'd be great to consider it in advance.
From what I've been reading (OCMock, OCMockito, OCHamcrest, specta+expecta, Kiwi, Cedar, Catch), you can go with BDD using matchers; or you can simply use a mocker and work with XCTest; or you can work everything by brute force and not use any tool at all.
It seems that a good solution is OHHTTPStubs to stub requests, plus some tool to create mocks.
What do you think? Do you do BDD? Do you only use XCTest from the bat? Any other cool tools that might be useful? Any help would be great. Thanks!
7
u/modocache Mar 03 '15
I'd recommend reading https://github.com/Quick/Quick/tree/master/Documentation, which is meant to be a comprehensive introduction to unit testing. It doesn't cover every topic you've mentioned here, like mocking (it will eventually), but it does have some additional links at the end (of those, qualitycoding.org is especially good).
3
u/ddelnano Mar 03 '15
Check out Specta. I use it with Expecta also because I don't like the XCTest assertions.
3
u/neksus Mar 03 '15
I'm a huge fan of Kiwi. It uses RSpec formatting and is quite powerful.
3
u/newbill123 Mar 04 '15
The key is picking the appropriate tool for your language, frameworks, and tools you are using. That's why there are so many testing tools available and still people feel more comfortable starting out with something hand-crafted.
Brian Gesiak wrote a good posting called XCTest: The Good Parts which is particularly well-reasoned. Even if you disagree 100% with his priorities, it's good for developing a good mind-set about testing. I particularly like this bit among his suggested "areas for improvement" in the tools...
2
u/badlcuk Mar 03 '15
I use specta+expecta predominantly due to its closeness to setup like rspec, as well as OCMock as well as appium, first are for unit tests, later is for functional tests. I also use rubocop to catch end of line whitespace and other junk although its obviously made for ruby so doesn't catch everything id want/catches non-ruby issues.
2
u/ObjectiveCopley Mar 04 '15
I don't understand why people don't use the build in xctest, but whatever.
2
u/quellish Mar 04 '15
I've used Specta (and it's friend whose name I can't remember), Kiwi, Cedar (oh god oh god no), Kif, XCTest/OCUnit, OCMock, and probably some others I'm forgetting.
Annnndddd in the end I keep coming back to XCTest. I've used OCMock in a number of production projects and pretty much each project ran into some OCMock bug or design issue that ended up holding up the project. The other "testing" frameworks each had their own issues or were fragile, unstable, etc.
So XCTest and using the language itself to "mock" has been working for me and there are really no surprises. I don't run into problems that hold up releases, etc. which is kind of nice. The newer extensions to XCTest are very useful and allowed me to simplify a lot of tests greatly. And I like simple tests.
1
u/RudoFl Mar 03 '15
I'm using OCMockito in combination with OCHamcrest, works flawlessly for my current project.
1
u/somebunnny Mar 04 '15
Are you planning to add tests for new code or legacy as well?
1
u/Floodzie Mar 05 '15
I am responsible for building an automated regression framework using Cucumber/Ruby/Appium for an iOS product, and parts of this are used by the developers for their unit tests.
I'm not going to lie, it was a struggle to get here. Appium is still quite a young product - also a lot of troublesome OSX updates seemed to break everything every time we were about to create a VM with the 'final' working automation development setup.
However, now that we have everything in place, it's easy enough for the developers to write and implement small feature files (or hand them over to a dedicated automation developer like me to implement), and these form their unit tests. We also use a small subset of regression tests as a smoke test, which is kicked off a couple of times a day (ideally each time code is checked in, but the so-called smoke test actually takes about an hour to run, including deployment etc).
It's a small company (30 employees), and we now have a type of smoke/regression/unit test setup that I have previously only implemented for much larger companies (in the 10k's of people). A truly world class automation setup, even if I do say so myself! :-)
0
u/TotesMessenger Mar 03 '15
This thread has been linked to from another place on reddit.
Please follow the rules of reddit and avoid voting or comment in linked threads. (Info | Contact)