ExUnit

ExUnit

Elixir的单元测试框架。

ExUnit的基本设置如下所示:

# File: assertion_test.exs # 1) Start ExUnit. ExUnit.start # 2) Create a new test module (test case) and use "ExUnit.Case". defmodule AssertionTest do # 3) Notice we pass "async: true", this runs the test case # concurrently with other test cases. The individual tests # within each test case are still run serially. use ExUnit.Case, async: true # 4) Use the "test" macro instead of "def" for clarity. test "the truth" do assert true end end

要运行上述测试,请从命令行使用该文件elixir。假设你命名了这个文件assertion_test.exs,你可以像下面这样运行它:

elixir assertion_test.exs

案例,回调和断言

ExUnit.CaseExUnit.Callbacks有关定义测试用例和设置回调的详细信息。

ExUnit.Assertions模块包含一组宏,用于生成具有相应错误消息的断言。

与Mix整合

混合是针对“Elixir”的项目管理和构建工具。调用混合测试在命令行中,将在每个匹配模式的文件中运行测试。* _ test.exs在测试项目目录。

您必须创建一个test_helper.exs文件中的test目录,并将代码放在那里的所有测试中。

类的最小示例。test_helper.exs文件将是:

# test/test_helper.exs ExUnit.start

Mix将test_helper.exs在执行测试之前加载文件。这对测试文件中requiretest_helper.exs文件没有必要。查看Mix.Tasks.Test更多信息。

摘要

类型

failed()state()

由ExUnit.Test和ExUnit.TestCase返回的错误状态

功能

configuration()

返回ExUnit配置

configure(options)

配置ExUnit

plural_rule(字)

返回word

plural_rule(word, pluralization)

注册pluralizationword

run()

API用来运行测试。如果ExUnit是通过ExUnit.start/1的。

开始(选项\ [])

启动ExUnit并在虚拟机终止之前自动运行测试。 它接受一组选项来配置ExUnit(与configure / 1接受的相同)

类型

failed()

failed() :: [{Exception.kind, reason :: term, stacktrace :: [tuple]}]

state()

state :: nil | {:failed, failed} | {:skip, binary} | {:invalid, module}

由ExUnit.Test和ExUnit.TestCase返回的错误状态

功能

configuration()

返回ExUnit配置。

configure(options)

配置ExUnit。

备选方案

ExUnit支持以下选项:

  • :assert_receive_timeout-所使用的超时时间assert_receive调用,默认为100毫秒;

  • :autorun-如果ExUnit在退出时默认运行。默认为true

  • :capture_log-如果ExUnit应默认跟踪日志消息并在测试失败时打印它们。对于单个测试,可以通过@tag capture_log: false.默认为false

  • :case_load_timeout-加载测试用例时使用的超时,默认为60_000毫秒;

  • :colors-一些格式化者要使用的颜色的关键字列表。到目前为止唯一的选择是[enabled: boolean]默认为IO.ANSI.enabled?/0

  • :exclude-指定通过跳过匹配筛选器的测试运行哪些测试;

  • :formatters-将打印结果的格式化程序默认为[ExUnit.CLIFormatter]

  • :include-指定通过跳过与筛选器不匹配的测试来运行哪些测试。请记住,默认情况下所有测试都包括在内,因此除非它们首先被排除在外,否则:include期权没有影响;

  • :max_cases - 并行运行的最大案例数量。它默认为System.schedulers_online * 2优化CPU绑定和IO绑定测试;

  • :refute_receive_timeout- refute_receive呼叫使用的超时时间,默认为100毫秒;

  • :seed - 用于随机化测试套件的整数种子值;

  • :stacktrace_depth- 配置格式化和记者使用的堆栈跟踪深度,默认为20;

  • :timeout-将测试的超时设置为60_000毫秒;

  • :trace-将ExUnit设置为跟踪模式,此模式设置:max_cases1并在运行时打印每个测试用例和测试。注意,在跟踪模式中,测试超时将被忽略。

plural_rule(word)

plural_rule(binary) :: binary

返回word...

如果没有注册,则返回附加“s”的单词。

plural_rule( word, pluralization)

plural_rule(binary, binary) :: :ok

注册pluralizationword...

如果其中一个已经注册,则被替换。

run()

API用来运行测试。如果ExUnit是通过ExUnit.start/1...

返回包含测试总数,失败次数和跳过测试次数的映射。

start(options \ [])

启动ExUnit并在虚拟机终止之前自动运行测试。 它接受一组选项来配置ExUnit(与configure / 1接受的相同)。

如果要手动运行测试,可以设置:autorunfalse...