// Automatically generate test scaffolding when user writes new code without tests or mentions needing tests. Supports unit, integration, e2e, and data tests for PHP and JavaScript. Invoke when user mentions "tests", "testing", "coverage", "write tests", or shows new untested code.
| name | test-scaffolding |
| description | Automatically generate test scaffolding when user writes new code without tests or mentions needing tests. Supports unit, integration, e2e, and data tests for PHP and JavaScript. Invoke when user mentions "tests", "testing", "coverage", "write tests", or shows new untested code. |
Automatically generate test scaffolding for untested code.
Activate this skill when the user:
Identify:
Unit Tests - For isolated logic:
Integration Tests - For component interaction:
E2E Tests - For user workflows:
<?php
namespace Drupal\Tests\mymodule\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\mymodule\Service\DataProcessor;
/**
* Tests for DataProcessor service.
*
* @group mymodule
* @coversDefaultClass \Drupal\mymodule\Service\DataProcessor
*/
class DataProcessorTest extends UnitTestCase {
/**
* The data processor service.
*
* @var \Drupal\mymodule\Service\DataProcessor
*/
protected $dataProcessor;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->dataProcessor = new DataProcessor();
}
/**
* Test processData method with valid input.
*
* @covers ::processData
*/
public function testProcessDataWithValidInput(): void {
$input = ['name' => 'John', 'email' => 'john@example.com'];
$result = $this->dataProcessor->processData($input);
$this->assertIsArray($result);
$this->assertEquals('John', $result['name']);
$this->assertEquals('john@example.com', $result['email']);
}
/**
* Test processData method with invalid input.
*
* @covers ::processData
*/
public function testProcessDataWithInvalidInput(): void {
$this->expectException(\InvalidArgumentException::class);
$this->dataProcessor->processData([]);
}
}
<?php
/**
* Tests for User_Manager class.
*
* @package MyPlugin\Tests
*/
namespace MyPlugin\Tests;
use WP_UnitTestCase;
use MyPlugin\User_Manager;
/**
* User_Manager test case.
*/
class Test_User_Manager extends WP_UnitTestCase {
/**
* User manager instance.
*
* @var User_Manager
*/
private $user_manager;
/**
* Set up test.
*/
public function setUp(): void {
parent::setUp();
$this->user_manager = new User_Manager();
}
/**
* Test get_user_data with valid user.
*/
public function test_get_user_data_with_valid_user() {
$user_id = $this->factory->user->create(
array(
'user_login' => 'testuser',
'user_email' => 'test@example.com',
)
);
$data = $this->user_manager->get_user_data( $user_id );
$this->assertIsArray( $data );
$this->assertEquals( 'testuser', $data['login'] );
$this->assertEquals( 'test@example.com', $data['email'] );
}
/**
* Test get_user_data with invalid user.
*/
public function test_get_user_data_with_invalid_user() {
$data = $this->user_manager->get_user_data( 99999 );
$this->assertFalse( $data );
}
/**
* Clean up test.
*/
public function tearDown(): void {
parent::tearDown();
}
}
/**
* Tests for userUtils module.
*/
import { formatUserName, validateEmail } from './userUtils';
describe('userUtils', () => {
describe('formatUserName', () => {
test('formats first and last name correctly', () => {
const result = formatUserName({ firstName: 'John', lastName: 'Doe' });
expect(result).toBe('John Doe');
});
test('handles missing last name', () => {
const result = formatUserName({ firstName: 'John' });
expect(result).toBe('John');
});
test('throws error for missing first name', () => {
expect(() => formatUserName({})).toThrow('First name required');
});
});
describe('validateEmail', () => {
test('validates correct email', () => {
expect(validateEmail('test@example.com')).toBe(true);
});
test('rejects invalid email', () => {
expect(validateEmail('not-an-email')).toBe(false);
});
});
});
<?php
namespace Drupal\Tests\mymodule\Functional;
use Drupal\Tests\BrowserTestBase;
/**
* Tests the user registration form.
*
* @group mymodule
*/
class UserRegistrationFormTest extends BrowserTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected static $modules = ['mymodule', 'user'];
/**
* Test user can register successfully.
*/
public function testUserRegistration(): void {
$this->drupalGet('/user/register');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains('Create new account');
$edit = [
'name' => 'testuser',
'mail' => 'test@example.com',
];
$this->submitForm($edit, 'Create new account');
$this->assertSession()->pageTextContains('Registration successful');
}
}
<?php
/**
* Tests for REST API endpoints.
*
* @package MyPlugin\Tests
*/
namespace MyPlugin\Tests;
use WP_UnitTestCase;
/**
* REST API test case.
*/
class Test_REST_API extends WP_UnitTestCase {
/**
* Test GET /wp-json/myplugin/v1/users endpoint.
*/
public function test_get_users_endpoint() {
// Create test user.
$user_id = $this->factory->user->create();
// Make REST request.
$request = new \WP_REST_Request( 'GET', '/myplugin/v1/users' );
$response = rest_do_request( $request );
$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertIsArray( $data );
$this->assertNotEmpty( $data );
}
}
/**
* E2E tests for user authentication.
*/
describe('User Authentication', () => {
beforeEach(() => {
cy.visit('/');
});
it('allows user to login successfully', () => {
cy.get('[data-test="login-button"]').click();
cy.url().should('include', '/login');
cy.get('[name="username"]').type('testuser');
cy.get('[name="password"]').type('password123');
cy.get('[type="submit"]').click();
cy.url().should('include', '/dashboard');
cy.contains('Welcome, testuser').should('be.visible');
});
it('shows error for invalid credentials', () => {
cy.get('[data-test="login-button"]').click();
cy.get('[name="username"]').type('wronguser');
cy.get('[name="password"]').type('wrongpass');
cy.get('[type="submit"]').click();
cy.contains('Invalid credentials').should('be.visible');
cy.url().should('include', '/login');
});
it('allows user to logout', () => {
// Login first
cy.login('testuser', 'password123');
// Then logout
cy.get('[data-test="logout-button"]').click();
cy.url().should('not.include', '/dashboard');
});
});
Generate basic test structure first:
For each public method:
Identify what needs mocking:
Check:
This skill complements the /test-generate slash command:
This Skill: Automatically triggered during conversation
/test-generate Command: Explicit batch generation
Example 1: Simple class
User: "I wrote this UserManager class, need tests"
[Shows class with 3 methods]
Skill: "I'll generate PHPUnit tests for your UserManager class.
I see 3 public methods: createUser(), getUser(), deleteUser().
I'll create tests for the happy path and error cases for each.
[Generates test file]
This gives you 80% coverage to start. You may want to add edge case
tests for boundary conditions."
Example 2: WordPress function
User: "How do I test this WordPress function?"
[Shows custom function that uses WP_Query]
Skill: "For WordPress functions using WP_Query, you'll want an
integration test. I'll generate a WP_UnitTestCase test that:
1. Creates test posts using the factory
2. Calls your function
3. Verifies the query results
[Generates test code]
The test uses wp_insert_post() to create test data, then cleans up
automatically in tearDown()."
testCreateUserWithValidData() not testCreateUser()Don't. Test public interface instead. If private method needs testing, consider extracting to separate class.
// Avoid static methods when possible
// If you must, test directly
$result = MyClass::staticMethod($input);
$this->assertEquals($expected, $result);
// Use transactions for rollback
protected function setUp(): void {
parent::setUp();
$this->database->beginTransaction();
}
protected function tearDown(): void {
$this->database->rollbackTransaction();
parent::tearDown();
}