Issue with GHUnit Testing for iPhone - iphone

I am working on Unit Test using third party framework GHUnit, created project added GHUnit framework and other framework which are needed.
I created one class called TestCases, in that import library GHUnit and class which need to write test case.
I need to write test cases for 40 classes.
Do i need to write all test cases in one single class.
Do i need to create each class for testCase?
If Yes then when i try to create new class in separate testCase1,testCase2....testCase40 it can't able to show those testCases1 testCase2 ...testCase40
IT shows me a tableview and run button and only first testCases methods, its not showing me remaining testCases Class method.
Please advice in this situation. What action i need to do for this
#Advance thanks you all.,

Separate unit testing (functionality testing) integration testing (complete system working testing)
UNIT TESTING: (for each of those 40 classes)
Usually write different test class for each of the class, so that if there is a single change in any of the class can test it by specifically running that particular class, so if there is one or 40 or 100s of class better write unit test for each of them and ensure there functionality.
In each class better write different test cases for testing different functionality, so that it will be easy to identity (for a third person, not the one who develop it and written test case for that) where the error come from, and manage them.
Each function better test only one case, write different test cases for testing different functional behavior of each functions. So it may result with 100 test cases in a single class for testing a class with 10 functions. But it is good.
INTEGRATION TESTING: (for testing depended functionality of 40 classes)
When come to integration testing, write test cases for different behavior of complete system, in a single class with different possibilities (test cases).
And finally “Spend more time for testing than coding”.
Also ensure the coverage of test cases for the code is between 90% to 100%.

Related

How to run test fixtures in order across multiple classes?

I have three classes all containing multiple tests. Each class has the annotation of TestFixture. I need each class to run the tests in order. (e.g. TextFixture1 runs all of its tests, then TestFixture2 runs all of its tests, and finally TestFixture3 runs all of its tests.) How can I accomplish this?
Use the OrderAttribute on each fixture, specifying the order in which you want the fixtures to run. Use the same attribute on each test method, specifying the order the test should run within the fixture.
See OrderAttribute in the docs for more info.

Xcode unit testing

I have never used Unit Testing and I understand the uses of it but I don't really know when and how to use it.
I would like to know when it's worth it to use Unit Testing, maybe with some examples.
The other answers tell when but not really how, so let me add an answer also.
When
Any time you are writing production code that you are going to keep, you should have Unit Testing for it. The most helpful training that I saw on this was the following 2-part video series:
Understanding Test Driven Development (part 1)
Unit Testing Best Practices with Roy Osherove (part 2)
The first five minutes or so are just intro so you can skip to the end of that.
How
I am using Xcode 7 with Swift.
Start a new project and add a Unit Test.
I am calling mine MyProject. If you open the MyProjectTests group in the Project Navigator, you will see that Xcode has already created a Unit Test file for you called MyProjectTest.swift.
You can delete all the example methods for now and add a new func to test your own class method. Be sure to add the line #testable import MyProject at the top. If your project name has spaces in it then replace the spaces with underscores. (For example, "My Example Project" would use #testable import My_Example_Project.)
I am following the naming pattern of testMethodNameBeingTested_Senario_ExpectedBehavior. Unit Test names must begin with "test".
I will do something like this:
import XCTest
#testable import MyProject
class MyProjectTests: XCTestCase {
func testSum_TwoNumbers_ReturnsSum() {
// Arrange (set up the needed objects)
let myClass = MyClass()
// Act (run the method you want to test)
let sum = myClass.sum(1, 2)
// Assert (test that the behavior is as expected)
XCTAssertEqual(sum, 3)
}
}
Of course, the build fails because we haven't added the MyClass class yet.
Add your class.
I am adding a Swift file to MyProject called MyClass.
class MyClass {
func sum(a: Int, _ b: Int) -> Int {
return a + b
}
}
Press the test button next to the Test Unit Class or method to run the test again and it should pass.
To see it fail (an important part of Unit Testing) you could do something like return 0 in the sum method of MyClass. Then you would see the following when you run test:
You can go back and fix this and then add more Unit Tests. You can also make other Unit Test files for different classes if you like. Just right click the MyProjectTest group in the Project Navigator and choose "New File" Then choose Test Case Class.
Related
Xcode UI Test example
You should almost always unit test and you should write code with unit tests in mind.
The extremists write tests even before writing the code (it's called TDD - Test Driven Development).
I'll give you a real life example: I recently had to code a sorted NSArray that supports "intervals". Meaning, the array should know how to insert an interval and keep it sorted.
For example, the array would look like this: [1-3, 5-9, 12-50]. In this example there are 3 intervals in the array, and as you can see they are sorted.
After I wrote my class (I called it IntervalsArray), I HAD to write tests to make sure that it works correctly and that I will not "break" it if I or someone else make changes to the code in the future.
Here are some example tests (pseudo-code):
Test 1:
- Create a new IntervalsArray
- Insert a new interval to the array
- (TEST) make sure the array has 1 object in it
Test 2:
- Create a new IntervalsArray
- Insert 2 intervals into the array: [1-3] and [5-9]
- (TEST) make sure there are 2 items in the array
- (TEST) make sure interval [1-3] comes before interval [5-9]
At the end I had something like 15 tests to cover every aspect of my new array.
Here's a good unit-testing with Xcode tutorial.
You can also write logic tests (which are more complicated than unit tests) to test your UI. Read a little about UIAutomation, which is Apple's way of testing UI. It's not perfect, but it's pretty good. Here's an excellent tutorial about this.
If you consider yourself a good programmer, you should write unit-tests for your code.
Write unit tests any time you write code that you'll have to maintain. That is, if you ever want to refactor anything — changing the code but keeping the behavior. Which is pretty much every bit of production code.
The counterexample of "Hello, World" is not to bother with code you plan to throw away. A "spike solution" is just to figure out how you might approach a problem. Once you've figured it out, throw it away and start again. Only this time, you start with tests.
Calling TDD "extremist" makes it sound irrational and impractical. In fact, once you learn TDD, it saves time/money.
See Unit Testing Example with OCUnit for an example of how TDD works.
Any time your writing an application that has classes, that are not your own. That is a good time to add unit tests, to test those classes.
All but the most basic apps will have their own classes, so its almost always a good idea to unit test.
If you are creating libraries that other programmers will use, or that you will use in multiple projects, those should always have unit tests.
Unit tests save you a lot of time when things change, for instance, a new version of the OS comes out, it is much better to test with the unit tests then to just test the app.

Can race conditions occur in TestNG #Test?

I have written a couple of tests, very identical but different in only one way. One test has an international address and the other has domestic address: DomesticAddress.scala & InternationalAddress.scala
DomesticAddress.scala extends another class ShipMethods.scala that has a #Test method. Here I am validating that DomesticAddress.scala has valid ship methods present. InternationalAddress.scala does not extend ShipMethods.scala.
Both test classes (DomesticAddress.scala and InternationalAddress.scala) have different users; the only similarity is that the address is stored in a val named 'address'.
When I run these two tests some times (and only some times) the test fails for DomesticAddress.scala because I see an international address in there.
Is it possible that there is a race condition happening in this scenario? My testng xml is preserving the order of tests, so this is more confusing that a race condition could occur especially since I am not sharing any resources among the tests.
Why does your ShipMethods have a #Test annotation on it? Is it a test class? If it's not a test class (as opposed to a class which you want to test), then it shouldn't have a #Test annotation on it.
Your classes under test shouldn't have test methods inside them.

Selenium junit tests - how do I run tests within a test in sequential order?

I am using junit with eclipse to write function tests.
When running an individual test it runs in the order that I have them set within the class.
Eg.
testCreateUser
testJoinUserToRoom
testVerify
testDeleteUser
However when I run this test as part of a suite, (so in a package) the order is random.
It will for example do the verify, then delete user then joinuserToRoom then Createuser.
My tests within the suite are not dependent on each other. However each individual test within a test is dependent on them being run in the correct order.
Is there any way I can achieve this?
Thanks.
You can't guarantee the order of execution of test methods in JUnit.
The order of execution of test classes within a suite is guaranteed (if you're using Suite), but the order of execution if the test classes are found by reflection isn't (for instance, if you're running a package in Eclipse, or a set of tests from maven or ant). This may be definable by ant or maven, but it isn't defined by JUnit.
In general, JUnit executes the test methods in the order in which they are defined in the source file, but not all JVMs guarantee this (particulary with JVM 7). If some of the methods are inherited from an abstract base test class, then this may not hold either. (This sounds like your case, but I can't tell from your description).
For more information on this, see my answer to Has JUnit4 begun supporting ordering of test? Is it intentional?.
So what can you do to fix your problem? There are two solutions.
In your original example, you've actually only got one test (verify), but you've got 4 methods, two setup (createUser, joinUserToRoom) and one teardown (deleteUser). So your first option is to better define your test cases, using a TestRule, in particular ExternalResource. ExternalResource allows you to define before/after behaviour for a test, similar to #Before/#After. However, the advantage of ExternalResource is that you can factor this out of your test.
So, you would create/delete the user in your external resource:
public class UsesExternalResource {
#Rule
public ExternalResource resource= new ExternalResource() {
#Override
protected void before() throws Throwable {
// create user
};
#Override
protected void after() {
// destroy user
};
};
#Test
public void testJoinUserToRoom() {
// join user to room
// verify all ok
}
}
For me, this is simpler and easier to understand, and you get independent tests, which is a good thing. This is what I would do, but you will need to refactor your tests a bit. You can also stack these Rules using RuleChain.
Your second option, if you really want to introduce dependencies between your test methods, is to look at TestNG, in which you can define dependencies from one test to another.
If they have a 'correct' order, then they are not multiple tests, but a single test that you have incorrectly annotated as being multiple independent tests.
Best practise would to rewrite them in approved junit style (setup - act - verify), supported by #Before or #BeforeClass methods that did any required common setup.
Quick workaround would be to have a single #Test-annotated method that called the other test methods in sequence. That becomes something like the preferred alternative if you are using Junit not to do strict unit testing, but something more like scenario-driven systems testing. It's not necessarily the best tool for such use, but it does work perfectly well in some cases.
Then, what you would have so far is have a single test:
#Test public void testUserNominalLifeCycle(...
which could then, if you are feeling virtuous, be supplemented by extra new tests like
#Test public void testUserWhoNeverJoinsARoom(...

Delay-loading TestCaseSource in NUnit

I have some NUnit tests which uses a TestCaseSource function. Unfortunately, the TestCaseSource function that I need takes a long time to initialize, because it scans a folder tree recursively to find all of the test images that would be passed into the test function. (Alternatively it could load from a file list XML every time it's run, but automatic discovery of new image files is still a requirement.)
Is it possible to specify an NUnit attribute together with TestCaseSource such that NUnit does not enumerate the test cases (does not call the TestCaseSource function) until either the user clicks on the node, or until the test suite is being run?
The need to get all test images stored in a folder is a project requirement because other people who do not have access to the test project will need to add new test images to the folder, without having to modify the test project's source code. They would then be able to view the test result.
Some dogmatic unit-testers may counter that I am using NUnit to do something it's not supposed to do. I have to admit that I have to meet a requirement, and NUnit is such a great tool with a great GUI that satisfies most of my requirements, such that I do not care about whether it is proper unit testing or not.
Additional info (from NUnit documentation)
Note on Object Construction
NUnit locates the test cases at the
time the tests are loaded, creates
instances of each class with
non-static sources and builds a list
of tests to be executed. Each source
object is only created once at this
time and is destroyed after all tests
are loaded.
If the data source is in the test
fixture itself, the object is created
using the appropriate constructor for
the fixture parameters provided on the
TestFixtureAttribute or the default
constructor if no parameters were
specified. Since this object is
destroyed before the tests are run, no
communication is possible between
these two phases - or between
different runs - except through the
parameters themselves.
It seems the purpose of loading the test cases up front is to avoid having communications (or side-effects) between TestCaseSource and the execution of the tests. Is this true? Is this the only reason to require test cases to be loaded up front?
Note:
A modification of NUnit was needed, as documented in http://blog.sponholtz.com/2012/02/late-binded-parameterized-tests-in.html
There are plans to introduce this option to later versions of NUnit.
I don't know of a way to delay-load test names in the GUI. My recommendation would be to move those tests to a separate assembly. That way, you can quickly run all of your other tests, and load the slower exhaustive tests only when needed.