mcpField
D
drupal-dtt

drupal-dtt

github.com/edutrul测试与质量7350 次安装21 周前更新
代码仓库

关于 drupal-dtt

面向 Drupal Test Traits(DTT)ExistingSite 测试的技能,可针对运行中的 Drupal 站点编写和运行冒烟、集成及回归测试,使用真实内容、配置和用户。

主要功能

  1. 01针对运行中的 Drupal 站点测试
  2. 02编写冒烟和集成测试
  3. 03断言 HTTP 状态及页面内容
  4. 04浏览页面并管理登录状态
  5. 05运行全部或指定 PHPUnit 测试

适用场景

  1. 01使用真实内容检查站点行为
  2. 02测试生产环境发现的问题回归
  3. 03运行指定的 ExistingSite 测试

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/edutrul/drupal-ai --skill drupal-dtt

安装后在 Claude Code 中输入/drupal-dtt确认已加载。

附带文件README.md

详细介绍

Drupal Test Traits (DTT) — ExistingSite 测试

使用场景

  • 针对真实运行中的 Drupal 站点进行测试
  • 使用类似生产环境的数据进行冒烟测试和集成测试
  • 需要真实内容、配置和用户时
  • 针对生产环境中发现的 Bug 编写回归测试

基本结构

// tests/src/ExistingSite/MyFeatureTest.php
namespace Drupal\Tests\my_module\ExistingSite;

use weitzman\DrupalTestTraits\ExistingSiteBase;

/**
 * Tests My Feature on an existing site.
 *
 * @group my_module
 * @group existing_site
 */
final class MyFeatureTest extends ExistingSiteBase {

  public function testHomepageLoads(): void {
    $this->drupalGet('/');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->pageTextContains('Welcome');
  }

  public function testArticleCreation(): void {
    // Create a user with role
    $user = $this->createUser();
    $user->addRole('editor');
    $user->save();
    $this->drupalLogin($user);

    // Create a node
    $node = $this->createNode([
      'type' => 'article',
      'title' => 'Test Article',
      'status' => 1,
    ]);

    $this->drupalGet('/node/' . $node->id());
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->pageTextContains('Test Article');
  }

  public function testFormSubmission(): void {
    $this->drupalGet('/contact');
    $this->assertSession()->statusCodeEquals(200);

    $this->submitForm([
      'name' => 'Test User',
      'mail' => '[email protected]',
      'message[0][value]' => 'Test message',
    ], 'Send message');

    $this->assertSession()->pageTextContains('Your message has been sent.');
  }

}

常见断言

// HTTP status
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->statusCodeEquals(403);
$this->assertSession()->statusCodeEquals(404);

// Page content
$this->assertSession()->pageTextContains('Expected text');
$this->assertSession()->pageTextNotContains('Missing text');

// Elements
$this->assertSession()->elementExists('css', '.my-class');
$this->assertSession()->elementTextContains('css', 'h1', 'Page Title');
$this->assertSession()->linkExists('My Link');
$this->assertSession()->linkByHrefExists('/my-path');

// Fields
$this->assertSession()->fieldExists('edit-title-0-value');
$this->assertSession()->fieldValueEquals('edit-title-0-value', 'My Title');

// Response headers
$this->assertSession()->responseHeaderContains('X-Drupal-Cache', 'HIT');

辅助工具

// Navigate to URL
$this->drupalGet('/path');
$this->drupalGet(Url::fromRoute('my_module.page'));

// Login
$this->drupalLogin($user);
$this->drupalLogout();

// Create content (automatically cleaned up after test)
$node = $this->createNode(['type' => 'article', 'title' => 'Test']);
$user = $this->createUser([], NULL, FALSE, ['mail' => '[email protected]']);
$term = $this->createTerm($vocabulary);

// Submit form
$this->submitForm(['field_name' => 'value'], 'Submit button label');

// Click link
$this->clickLink('Link text');

// Find element
$page = $this->getSession()->getPage();
$element = $page->find('css', '.my-selector');

运行测试

# Run all DTT tests
ddev exec vendor/bin/phpunit tests/

# Run specific test
ddev exec vendor/bin/phpunit tests/src/ExistingSite/MyFeatureTest.php

# Run with group filter
ddev exec vendor/bin/phpunit tests/ --group my_module

# Verbose output
ddev exec vendor/bin/phpunit tests/ --verbose

评论

支持 Markdown