0Pricing
Elixir & Phoenix: Scalable Backend Development · 课时

使用 StreamData 进行基于属性的测试

通过 StreamData 生成数百个随机输入,超越基于示例的测试,找出 ExUnit 示例遗漏的边界情况。

使用 StreamData 进行基于属性的测试 是 CoddyKit 上的免费 Elixir & Phoenix: Scalable Backend Development 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Elixir & Phoenix: Scalable Backend Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Elixir & Phoenix: Scalable Backend Development 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

What is Property-Based Testing?

Traditional tests check specific examples. Property-based testing describes a rule that should hold for all inputs, then generates many random inputs to try to break it.

It excels at finding edge cases you never thought to write by hand.

Adding StreamData

StreamData is the Elixir library for generating test data. Add it to your dependencies.

defp deps do
  [{:stream_data, "~> 0.6", only: :test}]
end

Importing the Helpers

Use ExUnitProperties in your test module to unlock the check all and property macros.

defmodule MyApp.MathTest do
  use ExUnit.Case
  use ExUnitProperties
end

Writing a Property

The property macro replaces test. Inside, check all draws values from a generator and runs the body for each.

property "reversing twice returns the original list" do
  check all list <- list_of(integer()) do
    assert Enum.reverse(Enum.reverse(list)) == list
  end
end

Built-in Generators

StreamData provides generators for common types:

  • integer(), float(), boolean()
  • string(:alphanumeric)
  • list_of/1, map_of/2
check all name <- string(:alphanumeric), age <- integer(0..120) do
  assert build_user(name, age).age >= 0
end

Combining Generators

Use tuple/1 or bind multiple generators in one check all to produce structured data.

check all {a, b} <- tuple({integer(), integer()}) do
  assert a + b == b + a
end

Transforming with map

StreamData.map/2 reshapes generated values, e.g. building positive integers from any integer.

positive = StreamData.map(integer(), &abs/1)
check all n <- positive do
  assert n >= 0
end

Filtering Inputs

filter/2 discards values that do not match a condition. Use it sparingly — heavy filtering slows generation.

non_zero = StreamData.filter(integer(), &(&1 != 0))
check all d <- non_zero do
  assert div(d, d) == 1
end

Shrinking Failures

When a property fails, StreamData shrinks the input — repeatedly simplifying it — to report the smallest failing example. This makes debugging far easier than a random huge value.

Choosing Good Properties

Effective properties describe invariants rather than restating the implementation:

  • Round-trip: encode then decode returns the original
  • Idempotency: applying twice equals applying once
  • Invariants: a sorted list is always ordered

When to Use It

Property tests complement, not replace, example tests. Reach for them on pure functions, parsers, serializers, and data transformations where many inputs share one rule.

Quick Check

Test your property-based testing knowledge.

Recap

You learned property-based testing with StreamData:

  • Describe rules that hold for all inputs
  • Use property + check all with generators
  • Combine, map, and filter generators
  • Shrinking reports the minimal failing case
  • Best for invariants, round-trips, and idempotency

It uncovers edge cases example tests miss.

常见问题解答

「使用 StreamData 进行基于属性的测试」课时是免费的吗?

是的 — 「使用 StreamData 进行基于属性的测试」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Elixir & Phoenix: Scalable Backend Development 课程的其余内容,请升级到 CoddyKit PRO。 Elixir & Phoenix: Scalable Backend Development 课程共包含 4 节课。

「使用 StreamData 进行基于属性的测试」这节课中我会学到什么?

通过 StreamData 生成数百个随机输入,超越基于示例的测试,找出 ExUnit 示例遗漏的边界情况。 你通过在浏览器中直接运行的动手代码来练习 Elixir & Phoenix: Scalable Backend Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Elixir & Phoenix: Scalable Backend Development 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Elixir & Phoenix: Scalable Backend Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「使用 StreamData 进行基于属性的测试」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Elixir & Phoenix: Scalable Backend Development 课中编写并运行代码吗?

能。每节 Elixir & Phoenix: Scalable Backend Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 使用 ExUnit 进行单元测试
  2. Phoenix 应用集成测试
  3. 模拟、存根与测试数据
  4. 使用 StreamData 进行基于属性的测试
← 返回 Elixir & Phoenix: Scalable Backend Development