Skip to content

Inheriting a suite should inherit its tests #1185

@soumyamahunt

Description

@soumyamahunt

Motivation

In XCTest, inheriting a XCTestCase class inherits all its tests creating new test case. This is especially useful when testing behaviors with different configurations. i.e.

class FeatureAWithConfigX: XCTestCase {
    override setup() {
        // setup config X
    }

    // Test behavior
    func testStuff() {}
}

class FeatureAWithConfigY: FeatureAWithConfigX {
    override setup() {
        // setup config Y
    }

    // No need to write tests for same behaviors
}

but this kind of usage is not possible with swift testing:

@Suite
class FeatureAWithConfigX {
    init() {
        // setup config X
    }

    // Test behavior
    @Test
    func stuff() {}
}

@Suite
class FeatureAWithConfigY: FeatureAWithConfigX {
    override init() {
        // setup config Y
    }

    // Write the tests again?
}

Proposed solution

Inheriting a suite should inherit all its tests as well, or any other functionality that could allow reusing all the tests defined in a test suite (may be parameterization at @suite level?)

@Suite
class FeatureAWithConfigX {
    init() {
        // setup config X
    }

    // Test behavior
    @Test
    func stuff() {}
}

@Suite
class FeatureAWithConfigY: FeatureAWithConfigX {
    override init() {
        // setup config Y
    }

    // Reuse same tests
}

Alternatives considered

Couldn't find any alternatives going through documentation.

Additional information

A real world usage of such pattern will be let's suppose you have written some logic available in ModuleA and you have already written tests for ModuleA. Now you are refactoring this into a ModuleB, but to avoid any breaking functionality you want to ship both ModuleA and ModuleB while ModuleB usage is controlled by some flag. To test both ModuleA and ModuleB have same behaviors you will need such functionality.

Metadata

Metadata

Assignees

No one assigned

    Labels

    discovery🔎 test content discoveryenhancementNew feature or requestpublic-apiAffects public APItriagedThis issue has undergone initial triage

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions