▸ CODESCAN
DocsSupportScanner
AI TEST GENERATION

AI Security Test Generation

For every vulnerability CodeScan finds, it can generate two tests: a failing test that proves the vulnerability exists (passes only when the code is vulnerable) and a passing test that proves the fix works (passes only after the fix is applied). This turns a finding into reproducible, committable test coverage.

How to use it

  • Run a scan and open any finding in the Detail view.
  • Click ⬡ Generate security tests below the recommendation.
  • CodeScan calls Claude with the vulnerability details and full file content to generate language-appropriate tests.
  • Copy the failing or passing test into your test suite.

Supported frameworks

LanguageFramework
TypeScript / JavaScriptJest
Pythonpytest
JavaJUnit 5 + Mockito
Gotesting package
RubyRSpec
PHPPHPUnit
C#xUnit
Rust#[test] functions

Example output — SQL injection

Failing test (proves vulnerability exists)

// Jest — this test PASSES when the code is vulnerable
describe("getUserById", () => {
  it("is vulnerable to SQL injection", async () => {
    const malicious = "1' OR '1'='1";
    // If vulnerable, this returns all users instead of throwing/returning null
    const result = await getUserById(malicious);
    // A vulnerable implementation returns data for any input
    expect(result).not.toBeNull(); // passes on vulnerable code
  });
});

Passing test (verifies the fix)

// Jest — this test PASSES after the fix is applied
describe("getUserById", () => {
  it("rejects SQL injection attempts", async () => {
    const malicious = "1' OR '1'='1";
    // After fix (parameterised query), this should return null or throw
    const result = await getUserById(malicious);
    expect(result).toBeNull(); // passes on fixed code
  });
});

Plan requirement

AI test generation uses the same feature gate as AI Auto-Fix and requires the Starter plan or above.

API endpoint

POST https://codesscan.com/api/scan/test-gen
Authorization: Bearer <token>
Content-Type: application/json

{
  "content":  "<full file content>",
  "filename": "user.service.ts",
  "language": "typescript",
  "finding": {
    "title":          "SQL Injection",
    "description":    "...",
    "line":           42,
    "snippet":        "const q = `SELECT * FROM users WHERE id = ${id}`",
    "recommendation": "Use parameterised queries",
    "cwe":            "CWE-89"
  }
}

# Response
{
  "framework":   "Jest",
  "failingTest": "// test code...",
  "passingTest": "// test code...",
  "explanation": "The failing test demonstrates that unsanitised input reaches the query; the passing test verifies parameterisation prevents it."
}