XCTestCases are code testing, function testing or functionality testing? - xctestcase

I am writing Unit test cases for my developed project. I am new in XCTest cases. But when I started writing XCTestCases most of the time I am just coping my function and paste them in XCTest case and check with XCTAssertTrur or XCTAssertNotNil or with other XCTestfunction. My confusion is only copy-paste is the process to test a function or any other things can be done in XCTests cases.

Related

Xcode swift tests not discovered

I'm using Xcode 9 and I'm trying to write unit tests following the Apple iOS tutorial here.
Problem is that my unit tests aren't being discovered, only the stub in another test project in the same solution.
The test in the screenshot should fail, but it doesn't as it's not discovered.
The testExample method is shown below
As you can see it has the little diamond indicating it has a test, but not next to the one I've created. Is there anything I'm doing wrong?
The answer to this was to prefix the function name with test
So the function now looks like
And the test is now discovered in the test explorer

How to do unit testing in Xcode while including a Metal library in the project

I have a library project (A) and a Metal library project (M). M is included into A in the "Copy files" phase. That introduces a build dependency, meaning that I can't build A for the simulator because it tries to build M first, and Metal is not supported on the simulator.
That's fine, but the problem is that A contains some unit tests, and when I try to test the project, I get this error message,
Logic Testing Unavailable. Logic testing on iOS devices is not supported. You can only run logic tests on the Simulator.
But I can't build for the Simulator because of the aforementioned dependency...
I read https://medium.com/the-sup-app/bare-metal-working-with-metal-and-the-simulator-70e085e3a45 -- perhaps this could help me removing the dependency of M in A for the simulator, but I'm trying to do this without Cocoapods, purely in Xcode.
Is there any workaround for this?
I ran into this exact same thing over the weekend. Tried to be a good citizen and include unit tests in my Metal project. ;-)
The only way out of this catch-22 is to not use XCTest for writing unit tests but create a separate target that runs as an app on its own. This new target then runs the unit tests.
In the old days I used GHUnit for this but I do not know if there is a suitable replacement for it these days.
Worst case scenario you can write your own little unit testing library that runs the XCTest macros.

Testing my DevelopmentPod from Example Project not working

I'm writing my own Pod and wanted to start unit Testing. But I can't figure out where in my Project to test.
I thought the Example Project was the right place to write my Tests, but from there I don't have internal Access to my DevelopmentPod.
I have to say it's the first XCode Project in which I want to unit test. So I'm not very familiar with it.
Any help on how unit Testing in Cocoapods should be set up is really appreciated.

Code coverage in Xcode without tests (for manual run)

Code Coverage is commonly used with tests in Xcode. I would like to use it for manually executed app. Can I do it, possibly with third-party tools?
For example: I build and launch the app on device, perform some actions with it and then look at code coverage results.
The solution was suggested by someone here: https://github.com/SlatherOrg/slather/issues/209
You could try having a XCUITest that sleeps forever, then manually use the app, and see if on termination the coverage files are generated.
I simply tried the solution and it worked perfectly for me:
class FooTests: XCTestCase {
override func setUp() {
super.setUp()
let app = XCUIApplication()
app.launch()
}
func testBalances() {
sleep(30)
XCTAssert(true)
}
}
After the test succeeded on XCTAssert(true), I could see the code coverage for the manual use cases. You can play around with the sleep(30) to match your requirement.
Probably you might have already figured it out, but it was possible before Xcode7. The way we achieved this was, to set the "Instrument Program Flow" and "Generate Test Coverage files" flags to Yes, on your project, and later add a "flush" code somewhere inside you application, to write the coverage data files. This "flushing" part actually writes the coverage data files, which can be used later by other tools such as gcovr or lcov, to get your coverage data. Once you interact with the app, either manually or through automated tests, the coverage data gets written.
However, with Xcode7, looks like the coverage data is limited to only Xcode unit tests. I am still trying to figure out, if there is any way to gather the coverage data, by interacting with the application manually, or through automated tests.
Code coverage will record which parts of your code your test have run. But you could Build some UITests which would preform some actions as you said. UI can record UI tests to repeat the actions you perform on the simulator then when you run the test it will repeat what you did. The coverage will then show which parts of the code where excited during the UITests.
Look for some info on UITesting in Xcode 7. There is a good demo in one of the developer sessions from wwdc15
https://developer.apple.com/videos/wwdc/2015/?id=406

Controlling order of unit test methods in OCUnit

I have been using OCUnit for unit testing in a project. I've managed to do all the setup required for OCUnit in Xcode 4.2 & successfully build my test cases.
How can I control the order (priority) for a test case? I see that test cases are built in alphabetic order of their name, i.e. if I have test method testA, testB, testC, they'll be executed in that same order.
What I need is for "testC" in the above example to be executed before "testB", as my method has some setup for variables & preferences for "testB", as well as core data entry, included in "testC".
Either your make your test cases self-contained, so that they can run on their own without pre-conditions, or you name your test methods accordingly as you mentioned.
test0First, test1Second
I would recommend to make them self-contained. It's a little overhead, but you can implement reusable methods in your test case (that don't start with test) to make your life easier.
If you want a framework with more features check out GHUnit

Resources