Spock Test Runner for VS Code
A VS Code extension that provides comprehensive test support for the Spock testing framework in Java projects. This extension integrates with VS Code's Test API to provide seamless test discovery, execution, and debugging capabilities for Spock tests.
Version: 0.0.2
Author: Lukas Zaruba
Inspiration: This extension was inspired by Daniel Micah's spock-test-runner but focuses exclusively on VS Code's Test API integration rather than CodeLens functionality.
Features
- Test Discovery: Automatically discovers Spock test classes and methods in your workspace
- Test Execution: Run individual tests, test classes, or all tests through VS Code's Test Explorer
- Debug Support: Debug Spock tests with full breakpoint support and variable inspection
- Build Tool Support: Works with Gradle projects
- Real-time Updates: Automatically updates test tree when files change
- Error Reporting: Detailed error messages with file locations for failed tests
- Output Streaming: Real-time test output in VS Code's Test Results panel
Requirements
- VS Code 1.85.0 or higher
- Java 11 or higher
- Gradle build tool
- Spock framework in your project
Installation
From VS Code Marketplace
- Open VS Code
- Go to Extensions view (Ctrl+Shift+X)
- Search for "spock-test-runner-vscode"
- Click Install
From Source
- Clone this repository
- Run
npm install
to install dependencies
- Run
npm run compile
to build the extension
- Press F5 to run the extension in a new Extension Development Host window
Usage
Test Discovery
The extension automatically discovers Spock tests in your workspace. Tests are organized in the Test Explorer panel under the following hierarchy:
- Workspace
- File (.groovy files)
- Test Class (classes extending Specification)
- Test Methods (feature methods)
Running Tests
You can run tests in several ways:
- Run All Tests: Click the play button in the Test Explorer
- Run Specific Test: Click the play button next to a specific test
- Run from Command Palette: Use
Ctrl+Shift+P
and search for "spock-test-runner-vscode" commands
- Run from Context Menu: Right-click on a test file and select "Run Spock Test"
Debugging Tests
- Debug All Tests: Click the debug button in the Test Explorer
- Debug Specific Test: Click the debug button next to a specific test
- Set Breakpoints: Click in the gutter next to line numbers in your test files
- Debug from Command Palette: Use
Ctrl+Shift+P
and search for "spock-test-runner-vscode" commands
Supported Test Patterns
The extension recognizes the following Spock test patterns:
class MySpec extends Specification {
def "should do something"() {
given:
// setup
when:
// action
then:
// verification
}
def "another test"() {
expect:
// simple assertion
}
}
Configuration
The extension automatically detects your build tool:
- Gradle: Looks for
build.gradle
file
- Maven: Looks for
pom.xml
file
Debug Configuration
The extension automatically creates debug configurations for your project. You can find them in .vscode/launch.json
:
{
"type": "java",
"name": "Debug Spock Tests (Groovy)",
"request": "attach",
"hostName": "localhost",
"port": 5005,
"projectName": "your-project-name",
"sourcePaths": [
"${workspaceFolder}",
"${workspaceFolder}/src/test/groovy",
"${workspaceFolder}/src/main/groovy"
]
}
Sample Project
A sample Gradle project with Spock tests is included in the sample-project
directory. This project demonstrates:
- Basic arithmetic operations testing
- User service testing with CRUD operations
- Error handling and exception testing
- Multiple test scenarios
To test the extension:
- Open the
sample-project
folder in VS Code
- Install the Spock Test Runner extension
- Open the Test Explorer to see discovered tests
- Run or debug the tests
Troubleshooting
Tests Not Discovered
- Ensure your test classes extend
Specification
- Check that your Gradle build tool is properly configured
- Verify that Spock dependencies are included in your project
- Check the Output panel for "Spock Test Runner" logs
Debug Not Working
- Ensure Java Extension Pack is installed
- Check that debug port 5005 is available
- Verify that your project has proper source paths configured
- Check the Output panel for debug-related error messages
- Ensure
gradlew
or gradle
command is available in your PATH
- For Maven, ensure
mvn
command is available in your PATH
- Check that your build files are valid
Development
Building from Source
# Install dependencies
npm install
# Compile TypeScript
npm run compile
# Watch for changes
npm run watch
# Run tests
npm test
Project Structure
├── src/
│ ├── extension.ts # Main extension entry point
│ └── testController.ts # Test controller implementation
├── sample-project/ # Sample Gradle project
├── .vscode/ # VS Code configuration
├── package.json # Extension manifest
└── README.md # This file
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments