Why my XCode unit test does not work at all? - iphone

I created a empty test project (static library type) with enabling the unit test.
But after that the product of the unit test is red as shown on the picture below.
Also I can't find a schema for the test and I can't even run the test.
Any one can help me out?

Press and hold the run button and select test. Or go Product>Test from the toolbar. It will then build your target you want to test and run the tests on that.

Related

Is there a way to run tests without doing a full build in Xcode?

Is there a way to provide a small test window without performing a complete build in Xcode? I know that eclipse gives you a small preview window at the bottom of the IDE but I can not figure it out for Xcode. I am mainly looking to test certain conditions and outputs that may not always print a value to the screen. Any suggestions?
Move your testable code into a framework that is accompanied by its own unit test bundle. Now testing involves just clicking the little run button next to a unit test. You can run a single test method or a whole suite, depending on your needs. When you run a test, the entire app does not need to be built — just the framework; and the app itself never runs and the Simulator never opens. And the results appear in the little Xcode Console pane at the bottom of the project window (and are preserved also in the Report navigator).
This is an excellent way to organize your app structure. I routinely use this kind of architecture.

Xcode does not recognize new test functions

I've just started writing unit tests and I've encountered an issue. So far I've dug through different blogs on google and stackoverflow but I haven't been able to resolve this.
Every time I write a function to be tested the run button (shaped like a diamond) does not appear. The only run buttons that do show up are the default functions created by Xcode when you create a new unit test target.
Here's an example of the issues.
Xcode screenshot
Could it be a bug or am I missing something?
test needs to be on the front of the function signature. ie: test"functionName"()

How to run specific tests from one class xcode uitesting

I'm creating ui tests in xcode using XCTest(Swift)
How can I run, for example, 2-3 tests from one class without launching the rest of the test from this class?
Is it possible to add some tests to suite or run and run from command line just necessary suite/run?
According to the XCTestSuite documentation there is an option to create a custom test suite, but I haven't found any examples of it.
You can adjust which tests you want to run in the scheme, under the Test menu. There will be a list of your test classes and tests in a disclosure triangle hierarchy in the Edit Scheme > Test window, where you can select or deselect tests. When you run the tests with that scheme selected, only the tests you selected will run.
You can use multiple -test-only params in your run
xcodebuild test [...] -only-testing:"superApp/SuperTestClass/test2" -only-testing:"superApp/SuperTestClass/test5"

Adding Logic Tests to the project in Xcode 4.5

I'm using xcode 4.5 and trying to add logic tests to the project. (This is actually a test, newly created project without unit tests). I'm following this guide:
http://developer.apple.com/library/mac/#documentation/developertools/Conceptual/UnitTesting/02-Setting_Up_Unit_Tests_in_a_Project/setting_up.html
So, what i've done so far:
Created a new project (View Based Application template)
Click on File -> New- CocoaTouchUnitTestBundle
That's it. The tests (as documentation states) are ready to use.
But I have the following problems:
The SenTestinKit.framework appears in red
If I press Test button issue navigator shows nothing:
If I select another debugger here(LLDB by default, trying to select GDB):
Xcode just hangs out:
P.S I also tried to remove SenTestingKit.framework (which is in red) and add new. Nothing changed.
What am I doing wrong ?? I suppose to see something like this(screenshot from apple guide):
Any help will be greatly appriciated!
Actually, your code works now.
I don't know why Xcode didn't initially add the SenTestingKit framework in correctly when you created the test bundle.
But now the code, "testExample" which was auto-generated for you, is running, and it is telling you to add in some real tests, by giving you an error. That's what STFail does.
So, if you take out that line, and start writing your own tests, you should be fine.

Running a single JUnit test in Eclipse

If I have a test suite with multiple tests, when I try to run a single unit test, either from the context menu of the code editor, or from the JUnit view, it seems to insist on always running the entire suite, rather than the single test. Is there a way to disable to change this behavior so that I can ask to to run that, and only that, test.
In the package explorer unfold the class. It should show you all methods. Right click on the one method you want to run, then select Run As -> JUnit from the context menu (just tested with Eclipse 3.4.1). Also selecting "Run" on a single entry in the JUnit-results view to re-run a test works in the same way.
Fastest way I know of:
Press Ctrl+Shift+↑ (moves cursor to current method declaration),
press Alt+Shift+x (or d for debug) then press t (hotkey for "Run JUnit Test"),
check test result,
press Alt+← to get back to the line of code you were before.
If you want to run all tests, you can skip steps 1 & 4.
In Eclipse 3.5, you can get around this by changing the test runner to JUnit 3. This will only work for JUnit 3 tests, not JUnit 4 tests. You can change the test runner by doing the following:
Right click on one of the test methods in the Outline explorer
Select Run As -> Run Configurations
On the 'Test' tab, select 'Run a single test'
In the Test Runner dropdown, select 'JUnit 3'
It may work in other versions as well.
This is possible in Eclipse Indigo with JUnit 4.8.2. You right click the method you want to unit test individually in the Outline pane, and select Run As -> JUnit Test.
You can even selectively right click a Test method name in the source editor and select Run As -> Junit Test.
Don't use Strg+F11 but the hotkey Alt+Shift+X -> T.
Then Eclipse will execute the current open file as a JUnit test. The green play button will only run the last chosen Run Configuration.
For me, it works well with Alt+Shift+X -> T.
I'll add to the others by including a highly productive keyboard only way that allows you to debug a single unit test (method).
Move your cursor to the method name by using either
Ctrl+Shift+Up or
Ctrl+Shift+Down or
Ctrl+o then type the name of the method
Once your cursor is on the method name (or right before it):
Alt+Shift+D -> T (Debug)
Alt+Shift+X -> T (Run)
After you run the test you can go back to where your cursor was by doing:
Alt+Back
You almost get REPL like behavior by:
Ctrl+Shift+Up and Alt+Shift+X -> T and Alt+Back
You can also quickly set a breakpoint:
Ctrl+Shift+B
Right click somewhere on the test method name in the file and select "Run" -> "Junit Test". I do it all the time with Kepler and JUnit 4.
To run only one test method.
In the (package explorer or project explorer) unfold the class. It should show you all methods. Right click on the one method you want to run, then select Run As -> JUnit from the context menu.
I had the same problem others have had using Eclipse 3.4.1 and JUnit 4 test runner -- couldn't run single test. But I found a suggestion somewhere else that resolved the problem. My test class was still extending junit.framework.TestCase. When I stopped extending TestCase, everything worked fine and I was able to run single JUnit tests with the JUnit 4 test runner. Of course, I needed to JUnit 4 annotations use static imports for the assert methods, but I had already done that -- I had just not removed the TestCase inheritance.
If you have many tests in more than one file, and you want to run only the tests in a particular file, you could right click that file, and select run as -> junit test.
Now, if you want to run only one test, contained in a file with many tests, my guess is (I dont have eclipse installed here) that the Outline view will list all test methods for a file, and you will probably be able to right click a single test method and execute it as a JUNit test.
Hope it helps.
For me, it also does not work in Eclipse 3.4.2 , although it worked in the previous releases (I have tried running single method from package explorer and single junit test from junit view)
I agree with the point about 3.4.2 no longer working the way it did in previous versions. Before you could right-click on a single test in a group of tests from the JUnit view and execute just that test. In the latest version it insists on always running all of them. Granted, the view does run the test you clicked on first and dumps the rest in a rollup called "Unrooted Tests". If anyone figures this out I'd like to know how to get around it.
Reading some of the comments here, it seems you might be interested in running the tests for the code you change as you change it, without losing focus on the code you are working on. There's an eclipse plugin for doing just that. See infinitest.
If by test you might even mean single test method I like to use "Run Tests of Selected Member" using a key binding.
When I am inside of a test method only that test method will be run – if I have the cursor in the class scope it will run the whole class. I changed the default binding to something easy like Ctrl+r since I use it a lot.
Please see:
(If you don't see that keys entry it might come from the MoreUnit plugin – which is great anyway and you might want to have it)