# 调试

软件开发中的测试是一个过程，其目的是评估应用程序的功能，以确定代码是否满足指定的要求，并确定问题，以确保产品具有预期的质量。

## 自动化测试

自动化测试遵循使用自动化工具测试软件以发现缺陷的过程。在这个过程中，程序员使用自动化工具执行测试脚本并自动生成测试结果。

## V中的测试

在V中，所有的测试文件都必须使用以下格式命名：`*u test.V`并且函数应该以`test`开头。

```go
// sum.v in subfolder sum
module sum

pub fn sum(a, b int) int {
    return a + b
}
```

```go
// sum_test.v
import sum

fn test_sum() {
    assert sum.sum(2, 3) == 5
     // assert sum.sum(2, 3) == 777 // => sum_test.v:6: FAILED assertion
}
```

要执行测试，应执行`v test_sum.v`。

### 练习

1. 测试JSON结构：

```go
import json

fn test_encode_customer(){
    customer := Customer{ first_name: "Vitor", last_name: "Oliveira" }
    expected := '{ "first_name": "Vitor", "last_name": "Oliveira" }'

    encoded_json := json.encode(customer)
    assert encoded_json == expected
}
```

1. 测试文件：

```go
import os

fn test_file_creation() {
    file_name := './new_file.txt'
    content := 'text'

    os.write_file(file_name, content)
    assert content.len == os.file_size(file_name)

    os.rm(file_name)
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://v-community.gitbook.io/v-by-example/cn/examples/section_4/testing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
