# テスト

ソフトウェア開発におけるテストは、コードが特定の要求を満たしていることを確認してアプリケーションが意図したとおりに動作するかどうかを評価したり、製品の品質が期待に沿っているかどうかを調べるために問題点を特定するプロセスです。

## テストの自動化

自動化されたテストは、問題点を検出する自動化ツールを用いてソフトウェアをテストするプロセスです。プログラマーはテストスクリプトを実行し、自動化ツールを用いてテスト結果を自動生成します。

## Vのテスト

Vのテストファイルはすべて`*_test.v`というファイル形式に揃え、関数名は必ず`test_*`の形式にしなければなりません。

```
// sum.v（sum/サブフォルダ内）
module sum

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

```
// 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/jp/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.
