0Pricing
Elixir & Phoenix: Scalable Backend Development · レッスン

StreamDataによるプロパティベーステスト

StreamDataで何百ものランダムな入力を生成し、例ベースのテストを超えて、ExUnitの例では見落としやすいエッジケースを見つけます。

「StreamDataによるプロパティベーステスト」はCoddyKit上の無料Elixir & Phoenix: Scalable Backend Developmentレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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によるプロパティベーステスト」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Elixir & Phoenix: Scalable Backend Developmentコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Elixir & Phoenix: Scalable Backend Developmentコースには全4レッスンが含まれています。

「StreamDataによるプロパティベーステスト」で何を学びますか?

StreamDataで何百ものランダムな入力を生成し、例ベースのテストを超えて、ExUnitの例では見落としやすいエッジケースを見つけます。 ブラウザで直接実行するハンズオンコードでElixir & Phoenix: Scalable Backend Developmentを演習し、24時間対応の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に戻る