Skip to content

Commit

Permalink
generate testsuite from test prompt file (#4)
Browse files Browse the repository at this point in the history
* implemented quick selector tool

* implement test prompt file (#2)

* Create npm-publish.yml

* Update npm-publish.yml

* Update npm-publish.yml

* fix workflow: use pnpm

* fix workflow: add set version to release tag

* workflow: add checkout fetch-depth: 0

* 1.0.2

* 1.0.3

* patch 1.0.2

* fix src import

* 1.0.3

* wip generate test from test prompt file

* working testsuite generator

* move testsuite generator to new directory

* refactor cli

* update project tests

* Tidy test file mode (#3)

* tidy cli code

* make gen command output steps json

* update README and package.json
  • Loading branch information
wuttinanhi authored Nov 18, 2024
1 parent 89e4f40 commit a4c911f
Show file tree
Hide file tree
Showing 31 changed files with 774 additions and 289 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,4 @@ screenshot.png
.gen/**
app.test.ts
gen.test.ts
*.test.ts
2 changes: 0 additions & 2 deletions .test/.gitignore

This file was deleted.

45 changes: 9 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# aitestgen

Generate testcases from natural language descriptions
Generate testcases from natural language descriptions.

A command-line tool that leverages AI to automatically generate test cases from natural language prompts. This tool helps developers quickly create comprehensive test suites by describing what they want to test in plain English.

Expand All @@ -16,44 +16,19 @@ yarn add -g aitestgen

## Usage

### Basic Command
Set OpenAI key

```bash
aitestgen [options] [prompt]
export OPENAI_API_KEY="<YOUR_KEY_HERE>"
```

### Options
Generate testsuite from test prompt file ([todo.xml](examples/testprompts/todo.xml))

```txt
Usage: index [options]
Generate test from prompting
Options:
-o, --out <path> Output path for generated test file (default: "app.test.ts")
-gd, --gendir <path> Directory to save generated cache (default: ".gen/")
-p, --provider <provider> Set model provider "openai" "ollama" (default: "openai")
-m, --model <model> Specify model to use (default: "gpt-4o-mini")
-oh, --ollamahost <url> Set Ollama endpoint (default: "http://localhost:11434")
-hl, --headless <bool> Set browser headless mode (default: true)
-t, --test Run test only (default: false)
-v, --verbose Verbose log (default: false)
-h, --help display help for command
```

### Examples

Generate new tests:
```bash
aitestgen -- go to http://localhost:3000 and fill the form then expect successful message
aitestgen gen -f examples/testprompts/todo.xml
```

Run generated tests using Vitest:
```bash
aitestgen --test
```
the generated output will be saved at `todo.testsuite.test.ts`

## Contributing

Expand All @@ -71,12 +46,12 @@ git clone https://github.com/wuttinanhi/aitestgen
yarn install
```

3. Run tests
3. Run project tests
```bash
yarn test
```

4. Link this package
4. Link this package to use locally
```bash
yarn link
```
Expand All @@ -87,10 +62,8 @@ yarn link
| Option | Description |
|--------|-------------|
| `start` | Start the program. |
| `test` | Test the specs |
| `gentest` | Test the generated code |
| `test` | Run project tests |
| `lint` | Lint codebase |
| `translate` | Translate test steps only |


## License
Expand Down
28 changes: 28 additions & 0 deletions examples/testprompts/todo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<testsuite>
<name>Todo Website Test</name>
<output>todo.testsuite.test.ts</output>
<language>typescript</language>
<translator>puppeteer</translator>
<provider>openai</provider>
<model>gpt-4o-mini</model>
<testcases>
<testcase>
<name>Should add a todo</name>
<prompt>
1. go to https://microsoftedge.github.io/Demos/demo-to-do/
2. add a todo "Cook Dinner"
3. expected added todo "Cook Dinner" in the list
</prompt>
</testcase>
<testcase>
<name>Should remove a todo</name>
<prompt>
1. go to https://microsoftedge.github.io/Demos/demo-to-do/
2. add a todo "Cook Dinner"
3. expected added todo "Cook Dinner" in the list
4. remove added to do
5. expected empty to do list
</prompt>
</testcase>
</testcases>
</testsuite>
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env npx tsx

import { main } from "./src/cmds/testgen.ts";
import { main } from "./src/cmds/cli.ts";

// disable node `module` warning
// (node:15327) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
Expand Down
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"author": "wuttinanhi",
"license": "MIT",
"type": "module",
"homepage": "https://github.com/wuttinanhi/aitestgen",
"repository": "https://github.com/wuttinanhi/aitestgen",
"npm.packageManager": "auto",
"bin": {
"aitestgen": "index.ts"
Expand All @@ -15,6 +17,7 @@
"@langchain/ollama": "^0.1.0",
"@langchain/openai": "^0.3.11",
"commander": "^12.1.0",
"fast-xml-parser": "^4.5.0",
"langchain": "^0.3.5",
"openai": "4.68.4",
"ora": "^8.1.1",
Expand All @@ -32,8 +35,9 @@
"start": "tsx index.ts",
"test": "vitest tests",
"gentest": "vitest run app.test.ts",
"lint": "tsc --noEmit --skipLibCheck --project tsconfig.json",
"translate": "tsx src/cmds/translate.ts",
"dev:quick": "tsx index.ts --verbose -- go to http://localhost:3000 and fill the form then expect successful message"
"lint": "tsc --noEmit --skipLibCheck --project tsconfig.json"
},
"devDependencies": {
"@types/node": "^22.9.0"
}
}
22 changes: 21 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions src/cmds/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Command, createCommand } from "commander";
import { GenCommand } from "./gen.ts";
import { addGenericOptions } from "../helpers/cli.ts";
import { PromptCommand } from "./prompt.ts";
import { TestCommand } from "./test.ts";

export async function main() {
const program = new Command();

program.description(
`A command-line tool that leverages AI to automatically generate test cases from natural language prompts.
This tool helps developers quickly create comprehensive test suites by describing what they want to test in plain English.`,
);

const promptCommand = new PromptCommand();
program.addCommand(promptCommand);

const genCommand = new GenCommand();
program.addCommand(genCommand);

const testCommand = new TestCommand();
program.addCommand(testCommand);

program.parse();

switch (program.args[0]) {
case "prompt":
promptCommand.parse();
promptCommand.run();
break;
case "test":
testCommand.run();
break;
case "gen":
genCommand.parse();
genCommand.run();
break;
default:
console.log("Unknown args:", program.args[0]);
process.exit(1);
}
}
Loading

0 comments on commit a4c911f

Please sign in to comment.