How HttpRunner Converts JSON/YAML to GoTest: Architecture and Current Limitations

HttpRunner's convert command provides a structured pipeline for transforming JSON/YAML test cases into Go test code via convert/main.go, but the actual GoTest generation logic in convert/to_gotest.go is currently a non-functional placeholder that returns an empty string.

The httprunner/httprunner repository includes a conversion subsystem designed to translate test cases between multiple formats. While the architecture supports JSON/YAML to GoTest conversion through the hrp convert CLI, the implementation remains incomplete. Understanding the current workflow reveals both the sophisticated loader infrastructure and the specific gaps awaiting community contribution.

The Conversion Pipeline Architecture

The conversion workflow follows a three-stage pipeline orchestrated by the TCaseConverter struct in convert/main.go. This design separates concerns between input parsing, optional configuration merging, and output formatting.

Loading the Source Test Case

The process begins with TCaseConverter.loadCase (lines 20‑38), which inspects the source file extension to select the appropriate deserializer. The method dynamically chooses between LoadJSONCase and LoadYAMLCase before unmarshaling the content into an in-memory hrp.TestCaseDef structure.

// conceptual flow based on convert/main.go
func (c *TCaseConverter) loadCase() (*hrp.TestCaseDef, error) {
    // Lines 20-38: detects format and loads into TestCaseDef
}

This abstraction allows the subsequent stages to operate on a normalized internal representation regardless of the original serialization format.

Applying Configuration Profiles

If the user provides a profile file via CLI flags, the system invokes overrideWithProfile (lines 98‑129) to mutate the loaded test case. This stage injects or overrides request headers, cookies, and other configuration parameters before the final formatting step, ensuring environment-specific values can be applied without modifying the source test files.

Dispatching to Target Formatters

The Convert method (lines 70‑78) acts as a router, selecting the appropriate formatter based on the --to flag value. When OutputTypeGoTest is specified, the method delegates to c.toGoTest().

// From convert/main.go lines 70-78
switch c.outputType {
case OutputTypeGoTest:
    return c.toGoTest()
// ... other cases
}

This dispatch logic correctly routes GoTest conversion requests to the dedicated handler, maintaining clean separation between conversion targets.

The GoTest Conversion Implementation Gap

Despite the robust loading and dispatch infrastructure, the actual code generation for Go tests remains unimplemented.

The Placeholder in to_gotest.go

The file convert/to_gotest.go contains only a stub method that fulfills the interface contract but produces no output:

// TODO: convert TCase to gotest case
func (c *TCaseConverter) toGoTest() (string, error) {
    return "", nil
}

When users invoke hrp convert ./test.yaml --to-gotest, the pipeline executes correctly through the loading and dispatch stages, but this method silently returns an empty string, resulting in no file generation.

Reverse Conversion Limitations

The complementary functionality in convert/from_gotest.go is similarly incomplete. The convert2GoTestScripts function immediately aborts with a warning message, indicating that converting existing Go tests back to JSON/YAML format is not yet supported.

Unused Template Infrastructure

The repository includes an embedded template file at convert/testcase.tmpl intended for Go test generation. However, the current toGoTest() implementation does not utilize this template, leaving the scaffolding prepared but inactive.

Using the Convert Command

While GoTest generation is pending, the conversion system fully supports transformations between JSON and YAML formats, demonstrating the pipeline's operational status.

Converting Between JSON and YAML

To convert a YAML test case to JSON format (the default behavior):

hrp convert ./example_test.yaml

This command successfully deserializes the YAML source and re-serializes it to JSON using the established loader infrastructure.

Attempting GoTest Conversion

To trigger the GoTest conversion path (currently a no-op):

hrp convert ./example_test.yaml --to-gotest

The command completes without error, but produces no output file because toGoTest() returns an empty string.

Expected Output Structure

Once implemented, the generated Go test files would follow the naming convention example_test_test.go, utilizing the .go extension in the target directory specified by --output-dir or the source file's location.

Summary

  • HttpRunner's conversion architecture in convert/main.go provides a complete three-stage pipeline: loading, profiling, and dispatching.
  • The TCaseConverter.loadCase method (lines 20‑38) handles both JSON and YAML deserialization into normalized hrp.TestCaseDef structures.
  • GoTest conversion is stubbed only—the toGoTest() method in convert/to_gotest.go contains a TODO comment and returns an empty string, preventing file generation.
  • Reverse conversion from Go tests to JSON/YAML is explicitly unsupported in convert/from_gotest.go.
  • Template infrastructure exists at convert/testcase.tmpl but remains unintegrated with the conversion logic.

Frequently Asked Questions

Is JSON/YAML to GoTest conversion fully implemented in HttpRunner?

No. While the command-line interface accepts the --to-gotest flag and the dispatch logic in convert/main.go (lines 70‑78) correctly routes to the formatter, the toGoTest() method in convert/to_gotest.go is a placeholder that returns an empty string without generating code.

Which files handle the conversion logic?

The primary orchestration occurs in convert/main.go, which manages case loading, profile application, and formatter selection. The specific GoTest formatter resides in convert/to_gotest.go, while cmd/convert.go handles CLI flag parsing and TCaseConverter initialization.

What is the expected filename for generated Go test files?

According to the conversion patterns in the codebase, generated Go tests would use the suffix _test.go appended to the original basename. For example, example_test.yaml would produce example_test_test.go in the specified output directory.

Can I convert existing Go tests back to JSON/YAML format?

No. The convert/from_gotest.go file explicitly aborts with a warning when attempting to load Go test files as source input. The convert2GoTestScripts function indicates this reverse conversion is not yet supported.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →