> For the complete documentation index, see [llms.txt](https://v-community.gitbook.io/v-by-example/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://v-community.gitbook.io/v-by-example/en/examples/section_4/testing.md).

# Testing

Testing in software development is a process that aims to evaluate the functionality of an application with an intent to find whether the code met the specified requirements or not as well as identifying the problems to ensure that the product has expected quality.

## Automated tests

Automated tests follow the process of testing the software using an automation tool to find the defects. In this process, programmers execute the test scripts and generate the test results automatically by using automation tools.

## Tests in V

In V, all test files have to be named with the following format: `*_test.v` and the functions should start with `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
}
```

To execute the test, you should run `v test_sum.v`.

### Examples

1. Testing JSON structures:

```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. Testing files:

```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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
