Coder Social home page Coder Social logo

atakankarsli / xcodeselectivetesting Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mikeger/xcodeselectivetesting

0.0 0.0 0.0 262 KB

Xcode selective testing: Run only tests relevant to the changeset.

License: GNU General Public License v3.0

Objective-C 0.95% Swift 99.05%

xcodeselectivetesting's Introduction

Xcode Selective Testing

Run only tests relevant to the changeset.

Swift

What is it?

“Insanity is doing the same thing over and over and expecting different results.”

Albert Einstein, probably

Imagine we have the following dependencies structure:

If the 📦Login module is changed, it would only affect the 📦LoginUI and the 📱MainApp.

Does it make sense to test all the modules if we know only the 📦Login module is changed? No. We can only run 50% of the tests and get the same results.

This technique saves time when testing locally and on the CI.

test-changed-stats-build

Prerequisites

  • Your project must have multiple targets or modules

Installation

Xcode

Add to Xcode as SPM dependency.

  • Open your project or workspace in Xcode
  • Select yout project in the file list in Xcode
  • In the right menu select "Project", open tab "Package Dependencies"
  • Select "+"
  • In the new window, paste [email protected]:mikeger/XcodeSelectiveTesting in the search field
  • Select project if necessary, put a checkbox on "XcodeSelectiveTesting" in the list
  • Click "Add Package"

Using Swift Package Manager

Add .package(url: "[email protected]:mikeger/XcodeSelectiveTesting", .upToNextMajor(from: "0.8.1")) to your Package.swift's dependencies section.

Use SPM to run the command: swift run xcode-selective-test.

Using Mint

mint install mikeger/[email protected]

Manually

  • Checkout this repository
  • Compile the tool: swift build -c release

Integration

Use case: Swift Package Manager-based setup

In case you are using Swift Package Manager without Xcode project or workspace:

Run swift test --filter "$(swift run xcode-selective-test . --json | jq -r ". | map(.name) | join(\"|\")")"

NB: This command assumes you have jq tool installed. You can install it with Homebrew via brew install jq.

Use case: Xcode-based project, run tests locally

  1. Install the tool (see Installation: Xcode)
  2. Select your project in the Xcode's file list
  3. Right-click on it and select SelectiveTestingPlugin
  4. Wait for the tool to run
  5. Run tests normally, SelectiveTesting would modify your test plan according to the local changes

Alternatively, you can use CLI to achieve the same result:

  1. Run mint run mikeger/[email protected] YourWorkspace.xcworkspace --test-plan YourTestPlan.xctestplan
  2. Run tests normally, XcodeSelectiveTesting would modify your test plan according to the local changes

Use case: Xcode-based project, execute tests on the CI

  1. Add code to install the tool
  2. Add a CI step before you execute your tests: mint run mikeger/[email protected] YourWorkspace.xcworkspace --test-plan YourTestPlan.xctestplan --base-branch $PR_BASE_BRANCH
  3. Execute your tests

How does this work?

1. Detecting what is changed

Git allows us to find what files were touched in the changeset.

Root
├── Dependencies
│   └── Login
│   ├── ❗️LoginAssembly.swift
│   └── ...
├── MyProject.xcodeproj
└── Sources

2. Build the dependency graph

Going from the project to its dependencies, to its dependencies, to dependencies of the dependencies, ...

Dependencies between packages can be parsed with swift package dump-package.

BTW, This is the moment your Leetcode graph exercises would pay off

2.5. Save the list of files for each dependency

This is important, so we'll know which files affect which targets.

3. Traverse the graph

Go from every changed dependency all the way up, and save a set of dependencies you've touched.

4. Disable tests that can be skipped in the scheme / test plan

This is the hardest part: dealing with obscure Xcode formats. But if we get that far, we will not be scared by 10-year-old XMLs.

Command line options

  • --help: Display all command line options
  • --base-branch: Branch to compare against to find the relevant changes. If emitted, a local changeset is used (development mode).
  • --test-plan: Path to the test plan. If not given, tool would try to infer the path.
  • --json: Provide output in JSON format (STDOUT).
  • --dependency-graph: Opens Safari with a dependency graph visualization. Attention: if you don't trust Javascript ecosystem prefer using --dot option. More info here.
  • --dot: Output dependency graph in Dot (Graphviz) format. To be used with Graphviz: brew install graphviz, then xcode-selective-test --dot | dot -Tsvg > output.svg && open output.svg
  • --verbose: Provide verbose output.

Configuration file .xcode-selective-testing.yml

It is possible to define the configuration in a separate file. The tool would look for this file in the current directory.

Options available are (see selective-testing-config-example.yml for an example):

  • basePath: Relative or absolute path to the project. If set, the command line option can be emitted.
  • testPlan: Relative or absolute path to the test plan to configure.
  • exclude: List of relative paths to exclude when looking for Swift packages.
  • extra/dependencies: Options allowing to hint tool about dependencies between targets or packages.
  • extra/targetsFiles: Options allowing to hint tool about the files affecting targets or packages.

Support

Supported operating systems:

  • macOS 12.0+ (Monterey) : Xcode 14.2
  • Linux: Swift 5.8

Contributing

Contributions are welcome. Consider checking existing issues and creating a new one if you plan to contribute.

License

See LICENSE

Authors

  • 🇺🇦 Michael Gerasymenko <mike (at) gera.cx>

If you like this product, consider donating to my hometown's charity project Monsters Corporation 🤝

xcodeselectivetesting's People

Contributors

mikeger avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.