I would like to create a library for iOS applications which uses UIKit. Furthermore I would like to create unit tests for this library. Unfortunately my tests do not work because of UIKit ([UIFont systemFontOfSize:12.0] to be precise).
According to Apple's Unit Testing Guide there are two types of test cases: logic tests and application tests. Application tests seem to be the correct type for tests involving UIKit related stuff but I did not find out about how to set up application tests for libraries. Has anybody ever had the same problem and was able to solve it?
Thanks a lot!
I found a solution to this. You must add a new target to your project that is simply an empty application. Then use that application as your test host following the instructions for doing this with a regular application like here:
Why does instantiating a UIFont in an iphone unit test cause a crash?
Related
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
I have a new proyect and I want to implement UITests, but I have the following problem when I initialize the controller...
Error
My viewController identifier is MainView, my storyboard target membership marked... This sounds simple but I donĀ“t find any solution...
Call
Thank you very much
OK...I think the problem is that the tutorial you mention (http://www.raywenderlich.com/101306/unit-testing-tutorial-mocking-objects) is meant for standard iOS unit tests, not for iOS UI tests. I am guessing that the two types of tests are set up very differently and items that work in one will not work in another...To follow this tutorial (watch out for the fact that it looks to use slightly older Swift syntax), look for the folder in your app called ApplicationNameTests, not ApplicationNameUITests and write your tests there.
I have a few app/ipa files. Using instruments ui automation i could perform actions using a js file and a terminal command. Did nt need the code/project of app file. But from Xcode 7 onwards UI Automation is deprecated. And apple have brought in UI testing. With the limited tutorials available in internet I could understand that UI testing can be implemented only on an Xcode project. It cannot be run in an pp file individually. Please do correct me if my understanding is wrong. And guide me on how to do it.
Thanks in advance :-)
Don't know exactly how you can write UI testing for APP/IPA files.
But in the sense of code needed, I would rather say no. As the UI testing uses view hierarchy to find the UI Elements and perform actions, e.g. app.buttons["Hi"].tap().
I have recently implemented Localytics to get a better understanding of how our users are using our app.
The integration guide is pretty straight forward. However the unit tests can't be built any longer, when I run them.
The error is familiar to me. This error usually happens if the tested class is not part of the test target membership.
But the SDK _OBJC_CLASS_$_LocalyticsSession provides only a .h file. In order to make it part of the target membership, I needed the .m file, which I don't have.
Has anyone else utilised Localystics and can advice me how to proceed this regarding? Thanks
UPDATE
coneybeare's answer has actually made me try this:
The test target is set to None. But if I change it to target the app itself:
Then it works. However everytime I intend to run the unit tests the whole app has to start in the simulator, which is very irritating (and slow). Am I missing something? How else can I associate the .a code libraries with the test targets?
I don't use Localytics, but inspecing the SDK downloads shows a few .a code libraries. Ensure those are associated with the test targets.
I am trying to implement XCTest as I have a bug in an iOS App which I don't seem to figure out so want to start building up get cases.
The App however, when loaded, will automatically connect to a server to update data. For the tests however, I would like this not to happen, as I need to clear the CoreData database and populate for each test.
Is there a way to know when e.g. building (on the target) if tests are going to be ran? Ie so I can use a flag to to leave out certain actions when testing?
Or should I just duplicate my normal target specifically for tests, and put in a flag that way?
(e.g. #if TESTING instead of #if DEBUG)
Not a direct answer to your question, but it might be a solution to your problem.
You could mock your class that does the server connection (ie fake the server connection).
You can do this by using OCMock that you can find here or OCMockito that can be found here.
Currently, I find OCMock easier to implement with XCTest than OCMockito. However, there might be some issues with OCMock as well, but those can be ironed out be looking at this site.