0Pricing
Elixir & Phoenix: Scalable Backend Development · 강의

StreamData를 사용한 속성 기반 테스트

StreamData로 수백 개의 무작위 입력을 생성하여 예제 기반 테스트를 넘어서는 방법을 배웁니다. 이를 통해 ExUnit 예제가 놓치는 경계 사례를 찾을 수 있습니다.

StreamData를 사용한 속성 기반 테스트은(는) CoddyKit의 무료 Elixir & Phoenix: Scalable Backend Development 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 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/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Elixir & Phoenix: Scalable Backend Development 강의 전체를 잠금 해제할 수 있습니다. Elixir & Phoenix: Scalable Backend Development 강의에는 총 4개의 강의가 포함되어 있습니다.

“StreamData를 사용한 속성 기반 테스트”에서 뭘 배우나요?

StreamData로 수백 개의 무작위 입력을 생성하여 예제 기반 테스트를 넘어서는 방법을 배웁니다. 이를 통해 ExUnit 예제가 놓치는 경계 사례를 찾을 수 있습니다. 브라우저에서 직접 실행하는 실습 코드로 Elixir & Phoenix: Scalable Backend Development을(를) 배우며, 24/7 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(으)로 돌아가기