การทดสอบโดยอิงคุณสมบัติด้วย clojure.test.check
เรียนรู้การเขียนการทดสอบที่แข็งแกร่งเพื่อตรวจสอบคุณสมบัติของโค้ดกับข้อมูลนำเข้าหลากหลายรูปแบบโดยใช้ `clojure.test.check`
การทดสอบโดยอิงคุณสมบัติด้วย clojure.test.check เป็นบทเรียน Clojure Functional Programming & JVM Backend Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Clojure Functional Programming & JVM Backend Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Clojure Functional Programming & JVM Backend Development มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Beyond Example Tests
Welcome to Property-Based Testing (PBT)! Traditional unit tests use specific examples to check if your code works. PBT takes a different approach.
Instead of examples, you define properties that your code should always uphold, no matter the input. Then, a PBT tool generates many diverse inputs to try and break those properties.
PBT vs. Example-Based Testing
Think of it this way:
- Example-Based Testing (like
clojure.test): "Does(my-add 2 3)return5?" You pick the inputs. - Property-Based Testing (with
clojure.test.check): "Is(my-add a b)always equal to(my-add b a)for any integersaandb?" The tool generatesaandb.
PBT is excellent at finding edge cases you might not think of manually.
Defining a Property with for-all
In clojure.test.check, you define a property using the for-all macro. It takes a vector of bindings (variables paired with generators) and a predicate (a function that should return true if the property holds).
Try running this simple property that checks if an integer generated is indeed an integer.
(ns my-project.core
(:require [clojure.test.check :as tc]
[clojure.test.check.properties :refer [for-all]]
[clojure.test.check.generators :as gen]))
(defn run-int-property-check []
(let [result (tc/quick-check 100
(for-all [x gen/int]
(<= x (inc x))))] ; Property: x is always <= x+1
(println "Property check result:" result)))
(defn -main [& args]
(run-int-property-check))Building with Generators
Generators (from clojure.test.check.generators, commonly aliased as gen) are functions that produce random data of a specific type. They are the heart of PBT inputs.
Common generators include gen/int, gen/boolean, gen/string, and many more. You can also combine them!
(ns my-project.core
(:require [clojure.test.check.generators :as gen]))
(defn generate-and-print []
(println "Random int:" (gen/generate gen/int))
(println "Random boolean:" (gen/generate gen/boolean))
(println "Random string:" (gen/generate gen/string {:max-size 10}))) ; Limit string size for display
(defn -main [& args]
(generate-and-print))Executing Property Checks
Once you define a property with for-all, you need to run it. clojure.test.check provides functions like quick-check and check.
tc/quick-check N property: Runs the propertyNtimes and returns a summary.tc/check property: Provides a more detailed result, especially useful when a property fails.
Here, we check if addition is commutative (a + b = b + a).
(ns my-project.core
(:require [clojure.test.check :as tc]
[clojure.test.check.properties :refer [for-all]]
[clojure.test.check.generators :as gen]))
(defn check-sum-property []
(let [sum-property (for-all [a gen/int b gen/int]
(= (+ a b) (+ b a)))] ; Addition is commutative
(println "Checking sum property...")
(let [result (tc/quick-check 100 sum-property)]
(println "Result:" result))))
(defn -main [& args]
(check-sum-property))Practical Property: Reversibility
A common and powerful property to test is reversibility. If you apply an operation and then its inverse, you should get back the original input.
Let's define simple encode and decode functions (which just reverse a string) and check if (decode (encode s)) always yields s.
(ns my-project.core
(:require [clojure.test.check :as tc]
[clojure.test.check.properties :refer [for-all]]
[clojure.test.check.generators :as gen]))
(defn encode [s] (apply str (reverse s))) ; Simple reverse string
(defn decode [s] (apply str (reverse s)))
(defn check-encode-decode-property []
(let [prop (for-all [s (gen/string-alphanumeric 0 20)] ; Limit string size
(= s (decode (encode s))))]
(println "Checking encode/decode property...")
(let [result (tc/quick-check 100 prop)]
(println "Result:" result))))
(defn -main [& args]
(check-encode-decode-property))Finding Minimal Failures (Shrinking)
One of the most valuable features of clojure.test.check is shrinking. When a property fails, it doesn't just give you the first failing input. It tries to find the smallest possible input that still causes the failure.
This helps you quickly pinpoint the root cause of a bug. Run this example: my-buggy-function has a subtle bug when input is 0.
(ns my-project.core
(:require [clojure.test.check :as tc]
[clojure.test.check.properties :refer [for-all]]
[clojure.test.check.generators :as gen]))
(defn my-buggy-function [x]
(if (= x 0) 100 x)) ; Bug: returns 100 if input is 0
(defn check-buggy-property []
(let [prop (for-all [x gen/int]
(> (my-buggy-function x) 0))] ; Property: result is always > 0
(println "Checking buggy property (expecting failure)...")
(let [result (tc/quick-check 100 prop)]
(println "Result (look for 'smallest' failing input):" result))))
(defn -main [& args]
(check-buggy-property))Creating Custom Generators
You're not limited to basic generators. You can compose them or transform their output to create generators for more specific data types using functions like gen/fmap and gen/such-that.
gen/fmap (functor map) applies a function to the value produced by another generator. Here, we create a generator for positive integers.
(ns my-project.core
(:require [clojure.test.check :as tc]
[clojure.test.check.properties :refer [for-all]]
[clojure.test.check.generators :as gen]))
(def gen-positive-int
(gen/fmap #(inc %) gen/nat)) ; gen/nat produces non-negative numbers, inc makes them positive
(defn check-positive-property []
(let [prop (for-all [x gen-positive-int]
(> x 0))] ; Property: x is always greater than 0
(println "Checking positive integer property...")
(let [result (tc/quick-check 100 prop)]
(println "Result:" result))))
(defn -main [& args]
(check-positive-property))Composing Generators
For complex data structures, you can combine multiple generators. gen/vector creates a vector of generated items, gen/tuple creates a fixed-size sequence, and gen/hash-map creates maps with generated keys and values.
Here's an example of generating simple 'person' data.
(ns my-project.core
(:require [clojure.test.check.generators :as gen]))
(def gen-person
(gen/hash-map :name (gen/string-alphanumeric 3 10)
:age (gen/choose 1 100)))
(defn generate-people []
(println "Generating 3 people:")
(dotimes [n 3]
(println " " (gen/generate gen-person))))
(defn -main [& args]
(generate-people))Check Your Understanding
Property-Based Testing fundamentally changes how we think about test inputs.
Recap: Robust Testing with PBT
In this lesson, we explored Property-Based Testing with clojure.test.check.
- You learned to define properties using
for-all. - We saw how generators (
gen/int,gen/string, etc.) create diverse inputs. - You used
quick-checkto run your properties. - We discussed the power of shrinking to find minimal failing cases.
- Finally, you learned to create and compose custom generators for complex data.
PBT is a powerful tool for writing more robust and reliable Clojure applications by testing behaviors across an infinite range of inputs!
เรียนรู้ Clojure ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การทดสอบโดยอิงคุณสมบัติด้วย clojure.test.check” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การทดสอบโดยอิงคุณสมบัติด้วย clojure.test.check” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Clojure Functional Programming & JVM Backend Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Clojure Functional Programming & JVM Backend Development มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การทดสอบโดยอิงคุณสมบัติด้วย clojure.test.check”
เรียนรู้การเขียนการทดสอบที่แข็งแกร่งเพื่อตรวจสอบคุณสมบัติของโค้ดกับข้อมูลนำเข้าหลากหลายรูปแบบโดยใช้ `clojure.test.check` คุณปฏิบัติ Clojure Functional Programming & JVM Backend Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Clojure Functional Programming & JVM Backend Development หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Clojure Functional Programming & JVM Backend Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การทดสอบโดยอิงคุณสมบัติด้วย clojure.test.check” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Clojure Functional Programming & JVM Backend Development นี้ได้ไหม
ได้ บทเรียน Clojure Functional Programming & JVM Backend Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ทรานสดิวเซอร์เพื่อการประมวลผลอย่างมีประสิทธิภาพ
- โมนาดและนามธรรมเชิงฟังก์ชัน
- การทดสอบโดยอิงคุณสมบัติด้วย clojure.test.check
- ลำดับแบบขี้เกียจและสตรีมไม่สิ้นสุด