How can you create the following structure:
CoreDataTests : XCTestCase
Entity1CoreDataTests : CoreDataTests
Entity2CoreDataTests : CoreDataTests
Explanation: I would like to write some tests related to core data (check), but I want to split them up into different test case classes, so in one test case class I would have tests related to the User entity, in another, the tests related to Comment entity. The catch here is that I'd like the two test case classes to share the setUp and tearDown methods implemented in CoreDataTests and just call them with super instead of having to copy-paste it around.
But since CoreDataTests is a test case class, it doesn't have a header file, so the EntityTest classes complain that they don't have a base class specified.
You can just create a subclass of XCTestCase as you would a normal class, with .h and .m files.
So just File Menu > New File > New Cocoa Class with XCTestCase as the super class.
You can then create new tests using that as the super class, however, you'll still need to import the superclass .h file as it's not added automatically.
I did this for exactly the same reasons, testing core data and having common methods to setup up the context.
One annoying thing though, it still shows up in the tests GUI, just with no tests.
Related
I have an XCTestCase subclass EndpointIntegrationTests that contains integration tests that exercise the client application's endpoints against the backend.
We have a number of testing backend environments. They share the same databases, so the tests in EndpointIntegrationTests should work equally well on all of the backends.
I'd like to be able to instantiate multiple instance of EndpointIntegrationTests in a test run, initialising each (or configuring after initialisation) with the specific backend it should test. I'd then instantiate the suite once for each backend, so that all tests are run against all backends. Perhaps a bit like this…
func addTestCaseInstances() {
for environment in ["SIT01", "SIT02", "NFT"] {
let testCase = EndpointIntegrationTests(environment: environment)
addTestCaseInstance(testCase)
}
}
Is there a mechanism in XCTest to allow this? I've only ever seen XCTestCase subclasses be automatically instantiated by the test harness. The harness only instantiates each subclass once.
Thanks.
User guide contains following:
Usually, an extension is instantiated only once.
It's not very clear when extension can be instantiated many times? I'm supporting test suite with multiple extensions and every extension stores it's state in class fields. Everything works fine, but can I rely on this or should I refactor this code to use ExtensionContext.Store?
Usually, an extension is instantiated only once. So the question becomes relevant: How do you keep the state from one invocation of an extension to the next?
I think this sentence shall highlight that the same instance of an extension might be re-used for multiple tests. I doubt that the instance might be replaced in the middle of a test.
Multiple instances of an extension might be instantiated when a test uses programmatic extension registration (with #RegisterExtension). In such case, the test class creates its own instance of the extension. JUnit cannot reuse this instance in other test classes. But an instance created by declarative extension registration (with #ExtendWith) might be used for multiple test classes.
So I have a script test.py
from nose.tools import with_setup
class Test:
#with_setup(setup_func, teardown_func)
def test(self):
print "Hello World"
Can I have setup_func() and teardown_func() defined in an init.py in the same directory as "test.py".
Basically, the objective is to have a common setup and teardown for a bunch of test cases.
There is nothing magical about setup_func and teardown_func. You can certainly do that by importing them from any module, if it called init.py or __init__. I would suggest you put it in some other module, say test_setup.py because you may not want to have those definitions at the package level of the test module, and "explicit is better than implicit" philosophy will be enforced.
ADDED: However, you have a bigger problem here: with_setup is useful only for test functions, not for test methods or inside of subclasses. As written, in your code, your setup function will not be called regardless where it is specified, see here.
If you intend to make your tests as class methods, you would have to rely on setUp and tearDown methods of the unittest. If you wish, you can inherit your test class from a special setup class and reuse this same special class again for different tests.
I have a baseClass1.h an d baseClass1.m
These have several public methods which are used by several base classes. What I am trying to do is to create a different implementation of same methods declarations.
After I have written baseClass2.m whose interface is baseClass1.h also.
Now in subclasses, how do I have methods do what I've defined in baseClass2.m instead of their respective definition from baseClass1.m
EDIT:
I duplicated the target in baseClass1 workspace to a new target. Both implementation files are exclusive to 2 targets. What I'm trying to do is to use different .m file with each target selection from xCode.
It is somewhat like changing the AP definitions. To explore the possibility to discard baseClass1.m for given now. Any way so that even if I delete baseClass1.m and program should still build
If you want two different implementations of the same class in two different targets then you can simply use two separate implementation files for the same class, and add each of them to one target only, e.g.
"BaseClass.h": the interface,
"BaseClassA.m": implementation of BaseClass, only in target A,
"BaseClassB.m": implementation of BaseClass, only in target B.
An implementation file need not have the same name as class, that is just a (useful) convention.
You need create #protocol for common interface.
Implement it's methods in baseClass1 and baseClass2.
And then you can subclass any base class you wish.
You can use protocol to declare common interface.
baseProtocol.h:
#protocol your_protocol <NSObject>
#optional
- (void)methodA;
#required
- (void)methodB;
#end
baseClass1.h:
#import "baseProtocol.h"
#interface baseClass1 : NSObject<your_protocol>
#end
baseClass2.h:
#import "baseProtocol.h"
#interface baseClass2 : NSObject<your_protocol>
#end
Technically this is possible: easy, in fact.
It is also a Bad Idea™.
Why is it a bad idea? Because it is unexpected. It is very unexpected. So unexpected, that some people didn't think it possible, many more didn't even understand what you where asking.
By using this anti-pattern, you are dooming future developer to waste hours trying to figure out what you did, and why you did it.
In this example, just use a protocol, fake it out by using a cluster class, or just support both implementations in a single class using a flag to switch between the two. Don't make things harder on everyone just because you can be clever with the build system.
I am writing Objective-C unit tests in Xcode. I would like to extend my base class so I can reuse my setUp/tearDown methods. I created a header and put the imports there, since Xcode defaults to making just an implementation file.
Extending the base class works fine, except that all of its test methods are run a second time (or n times, depending on number of children).
My question is if this is the appropriate practice, or should I try something like base classes with no test methods?