Xcode: unit tests are misbehaving - ios

I'm working on an app for iOS. I have 3 targets:
App — the app itself
AppTests — unit tests
AppUITests — UI tests
The third one is currently empty, but I intend to use it later. There are lots of tests in the second one.
Before yesterday, when I pressed Cmd+U, Xcode would happily run all tests for me (on a simulator, usually), and give me comprehensive report in the Debug area down below.
All of a sudden that changed. Now I see output from unit tests (AppTests) for less than a second, then it disappears, and a bit later the output from UI tests appears instead — which is completely useless, since there are no UI tests.
I'm not OK with this. UI tests sometimes print valuable information (not normally, but sometimes I use that). I need to see this output. And I kinda can: I see that it's still there in the log file, like DerivedData/App/Logs/Test/<long-guid-here>/Session-2016-06-16_18\:34\:44-EjeDwZ.log, along with lots and lots of other information that I don't understand. And there is another file, like Session-2016-06-16_18:34:44-Wlv6KP.log, in the same folder, with, as it seems, output from UI tests.
I want to see both, as it was before. I'm not OK with grepping through the giant log file.
What I tried:
Removing DerivedData folder entirely.
Cloning the same project from git into a new directory and starting it from there.
Reverting to an earlier version of the code, which worked fine before.
Removing ~/Library/Caches/com.apple.dt.*.
Trying tests on a different simulator.
Doesn't matter.
Another symptom is that the "Tests" panel in Xcode doesn't work anymore. It still correctly displays test suites and tests themselves, but small buttons to the right of tests, intended to run them, don't do anything. I still can run the suite by right-clicking on it and choosing Run AppTests (and I see the output in that case), but not by clicking on buttons. Also, I can run individual tests by clicking on the buttons in the code editor, to the left of test functions.
Does anybody have any idea how to fix it?
Xcode 7.3, Swift, if it matters.
Update:
After fiddling with "Edit Scheme" — specifically, changing "Debug" to "Release", running tests, and then changing back — "Run" buttons in the Tests Navigator suddenly started working. Issue with two different logs still remains, and I have no idea what to make of this.
Update 2:
"Run" buttons stopped working AGAIN, for no obvious reason.
Update 3:
OK, I figured out how to view tests output in the report instead of debug area.

Related

Errors in UI tests have no location in Xcode (<unknown>:0)

I recently added a UI Testing target to our existing project (which already has an iOS app target and a couple of other testing targets). Everything is working as expected and UI tests run without problems, but if a test fails the errors are never displayed in the file they occur and the Test Report shows them at <unknown>:0 while the Issue Navigator shows them at <unknown>:
It does mark the correct test as failed, but doesn't shown an error inside the editor. I looked through the build settings but couldn't find anything out of the ordinary.
Naturally, this makes debugging the tests much more annoying and harder. The usual clean/derived data/xcode restart magic hasn't done anything.
Update: After investigating further, it turns out that this is NOT a problem for assertion failures, which show up correctly.

Including a Playground in my project

I'm working in Xcode 9 on a Swift project, and I'm in table flipping mode with this.
Here's my end goal: put my tests next to my logic in a playground so that as I write the logic, I can see the tests pass in close to real time.
I will also accept how to put my tests in a playground by themselves, I just don't want to be running tests on a simulator for every little thing.
My problems in trying to get this to work are many. First and foremost, I can't find a way to add a playground directly to my project/workspace. When I right click on a group in the project navigator and select 'New file...' I get this:
As you can see, no option for playground. Filtering also returns no results. If I press the plus button at the bottom of the project navigator, the same dialog (with no playground option) pops up.
I thought it's in another menu so I went to the File menu, and in "New >" I find an option for creating a playground. I select a blank playground from the chooser, choose where to save it (strange that it wanted to save it under 'Unsaved Xcode Documents') and a basic playground comes up in a new tab. Odd thing I notice, though, is that there's no longer a play button or scheme/simulator selector at the top on this tab.
It now doesn't recognize ANY modules other than Apple provided ones! And sure enough, creating the playground didn't actually add it to my workspace. I guess I have to manually add it to my project. Upon doing so, it will recognize my pods but none of my project files! Any class I try to use gives me an Unrecognized identifier. Trying to import my project doesn't work either, it just gives me a No module.
I tried clearing DerivedData, I tried cleaning, I tried restarting Xcode, I even made sure I had an iPad Pro in the list of simulators (since that seems to matter for some people).
In frustration, I deleted the playground, and resolved just to deal with running my tests constantly. But now, my project has persistent errors! Despite moving the playground to the trash, Swift continues to error saying 'Unresolved identifier' for that playground!
As the excellent Brandon Williams says in his talk at FrenchKit, a playground "does not get access to anything in the application target." Which is ridiculous, but hey, what are you gonna do. So the answer is: there is currently no way to do this the way I asked.
It IS possible, however, to achieve this functionality if your logic is in its own framework/cocoapod. That is, move the logic you want to test out of your app target and into an outside library that your app, subsequently, imports. This will allow your playground to import it as well, and you can test to your heart's content.

Xcode 8.3.2 keeps on building after every keystroke

Whenever I try to change anything in my source files, the Xcode builds the entire project.
And since, I am in between writing the code, its obvious that there are errors (like "NSLog(#"ABC")") will throw an error if written halfway like this "NSL").
If I leave it idle, it fails for 5 to 10 times more and finally I see build succeeded. And again, when I try to change anything, it rebuilds on every keystroke and fails.
I have tried unchecking, "show live issues", "continue building after errors". Even a deep clean (command+shift+alt+k). After cleaning, its ok, but when I rebuild again, the same nut job building process continues.
This issue is not related to IBDesignables also. I have tried to uncheck "Automatically refresh views" in the storyboard and it did not work.
Any help?
Looks like, I can't open my storyboard as a different window, not even in a new tab. I used to have one of my windows for the source code and one for storyboard only. This made the switch between storyboard and source code much faster. But may be because of IBDesignables, its not possible anymore.
The problem is partially solved. Even if I open my storyboard now, it shows the same behavior of rebuilding again and again. But atleast, I can write my code in peace now with only source code files open in different tabs/windows.

Logging and breakpoints not working in XCTests

I have a large iOS project, and my problem is that, when running XCTests:
Breakpoints within the app, or within the tests themselves, are not hit
NSLogs/prints from within the app are not visible within the test log, but logs from within the tests are visible
I have another iOS project within the same workspace, and breakpoints and logging work fine.
The iOS project in question was originally created on an old version of Xcode (circa 2012, unsure which version exactly); and I have seen this comment elsewhere from somebody having seen this issue with an old .xcodeproj.
The project itself is hybrid Obj-C/Swift, uses several Cocoapods, a watchkit extension, and tests divided into three targets.
Given my project's complicated configuration, I would prefer to avoid beginning again with a fresh .xcodeproj and try to mirror exactly the configuration of my faulty one.
What could the reason for this fault be and what might I change within my existing .xcodeproj's configuration to fix it?
Select your scheme, go to Edit scheme... and tick the Debug executable option under the Test > Info pane.
The debugger will attach to both your tests and your target application, and breakpoints will be hit on both parts of your project.
Note that when a breakpoint is hit in your target application, your test is still running and may time out, killing both applications.
The culprit was DEPLOYMENT_POSTPROCESSING = YES in the project file.
This has to do with how your test scheme is set up. I was having the same issue and I fixed mine by turning Debbuging "ON" for the test executable.
I made a new scheme for my UI tests, so I assumed the debugger wasn't attaching to the new UI tests scheme when no breakpoints were hit. However, this post
had the correct answer for me.
In my case breakpoint was not hit in just one callback, so I changed "Swift Compiler" "Optimization Level" to "No optimization" in project target build settings for debug, and it started working in Xcode 9.3 as well as AppCode.
For breakpoint not working case check which Optimization Level you have, if you have the new -Os breakpoints start acting weird, change to -None or No Optimization in debug mode whatever option you have
Problem with logs not appearing in a debug area, in my case, was due to the OS_ACTIVITY_MODE argument which was set to "disable" in Scheme -> Test -> Arguments
I tried everything in the replies as of this date, but in the end the only thing that worked in my case was to remove the target and recreate it again.

Unit testing problems and crashing with XCode 5 and Kiwi/XCUnit

I'm having many issues with unit testing in XCode 5 and want to verify if anybody else if having the same problems (and any possible solutions...) I'm using the Kiwi framework developing for ios7 and its a new project therefore is using XCUnit underneath. I'm on a 2011 macbook air on OSX 10.8
The first time I run a test on startup, it runs all tests, even if I only select one case/test class - it even runs all the disabled ones.
Upon adding new unit tests, there is ~1/3 chance that XCode crashes on running all tests.
Clicking on failed unit tests very rarely takes me to the failed unit tests. It acts as if the code has been deleted.
When unit tests have been fixed, errors are often left over both in the issue navigator and in the editor however test is now reporting to succeed.
Upon successful running of all unit tests Xcode often reports that Tests have failed however all show as successful in the navigator.
Unit tests quite regularly get 'stuck' and execute forever. Its then impossible to run/build anything else until have restarted xcode.
Anybody with a similar setup having the same issues? Any solutions? What a bloody mess.
It seems that Xcode 5.1 (DP) will solve your issues.
Yes I get similar problems, also using Kiwi. No idea if its Kiwi related, but certainly I experience crashes when running unit tests, perhaps about 1 in 5 runs, and more often if the program hits a breakpoint and I run the tests again with Cmd-U.
I haven't been using the 'test single' option, so can't comment there.
The errors left over problem appears to be generic to unit tests, not just Kiwi. If you delete the block that contains the left over error, wait a few secs for Xcode to recompile in the background and paste it back it, you'll get rid of it. Or exit and restart Xcode if you have a bunch of them.

Resources