الاختبار القائم على الخصائص باستخدام StreamData
تجاوز الاختبارات القائمة على الأمثلة عبر توليد مئات المدخلات العشوائية باستخدام StreamData للعثور على الحالات الحدية التي قد تفوتها أمثلة ExUnit.
الاختبار القائم على الخصائص باستخدام StreamData درس مجاني في Elixir & Phoenix: Scalable Backend Development على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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}]
endImporting 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
endWriting 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
endBuilt-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
endCombining 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
endTransforming 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
endFiltering 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
endShrinking 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 allwith 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) وفتح باقي دورة Elixir & Phoenix: Scalable Backend Development، انتقل إلى CoddyKit PRO. تتضمن دورة Elixir & Phoenix: Scalable Backend Development 4 دروس في المجموع.
ماذا ستتعلم في «الاختبار القائم على الخصائص باستخدام StreamData»؟
تجاوز الاختبارات القائمة على الأمثلة عبر توليد مئات المدخلات العشوائية باستخدام StreamData للعثور على الحالات الحدية التي قد تفوتها أمثلة ExUnit. تتمرن على Elixir & Phoenix: Scalable Backend Development مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Elixir & Phoenix: Scalable Backend Development؟
لا تُشترط خبرة سابقة. Elixir & Phoenix: Scalable Backend Development على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «الاختبار القائم على الخصائص باستخدام StreamData»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Elixir & Phoenix: Scalable Backend Development هذا؟
نعم. كل درس في Elixir & Phoenix: Scalable Backend Development يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- اختبار الوحدات باستخدام ExUnit
- اختبار التكامل لتطبيقات Phoenix
- المحاكاة وStubs وبيانات الاختبار
- الاختبار القائم على الخصائص باستخدام StreamData