LogoLogo
  • V by Example
  • en
    • examples
      • section_3
        • Functions
        • Methods
        • Arrays
        • Struct
      • section_1
        • Primitives
        • Keywords
        • Variables
        • Formatted Print
        • Strings
        • Comments
      • section_4
        • Files
        • JSON
        • Testing
        • Array Functions
      • section_2
        • Operators
        • Looping Constructs
        • If statement
        • Match
  • V por exemplos
    • Contribuindo
    • examples
      • section_1
        • Comentários
        • Primitivos
        • Palavras-chave
      • section_2
        • Operadores
  • V mit einem Beispiel
    • examples
      • section_3
        • Methods
        • Struct
        • Functions
        • Arrays
      • section_4
        • Testing
        • Array Functions
        • JSON
        • Files
      • section_2
        • Looping Constructs
        • If statement
        • Match
        • Operators
      • section_1
        • Primitive Datentypen
        • Schlüsselwörter
        • Formatted Print
        • Strings
        • Kommentare
        • Variables
    • CONTRIBUTING_de
  • V dengan Contoh
    • examples
      • section1
        • Variabel
  • 通过例子学V语言
    • 例子
      • section_3
        • 函数
        • 方法
        • 数组
        • 结构体
      • section_1
        • 基本类型
        • 关键字
        • 变量
        • print系列函数
        • 字符
        • 注释
      • section_4
        • 文件
        • JSON操作
        • 调试
        • 数组函数
      • section_2
        • 运算符
        • 循环结构
        • If表达式
        • 匹配
  • コード例で学ぶV言語
    • examples
      • section_3
        • 関数
        • メソッド
        • 配列
        • 構造体
      • section_1
        • プリミティブ型
        • キーワード
        • 変数
        • 書式付き出力
        • 文字列
        • コメント
      • section_4
        • ファイル操作
        • JSON操作
        • テスト
        • 配列の関数
      • section_2
        • 演算子
        • ループ
        • If文
        • マッチ
  • Changelog
  • Contributing
  • Documentation Style Guide
Powered by GitBook
On this page
  • 自动化测试
  • V中的测试
  • 练习

Was this helpful?

  1. 通过例子学V语言
  2. 例子
  3. section_4

调试

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

自动化测试

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

V中的测试

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

// sum.v in subfolder 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结构:

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. 测试文件:

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)
}
PreviousJSON操作Next数组函数

Last updated 5 years ago

Was this helpful?