การทดสอบแบบมีพารามิเตอร์และแบบไดนามิก
เขียนการทดสอบที่มีประสิทธิภาพซึ่งทำงานกับชุดข้อมูลหลายชุด โดยใช้การทดสอบแบบมีพารามิเตอร์และสร้างการทดสอบแบบไดนามิกขณะทำงาน
การทดสอบแบบมีพารามิเตอร์และแบบไดนามิก เป็นบทเรียน Testing Mastery: JUnit, Mockito & Integration Tests ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Testing Mastery: JUnit, Mockito & Integration Tests และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Testing Mastery: JUnit, Mockito & Integration Tests มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Data-Driven Testing Intro
Imagine you need to test a function with many different inputs. Writing a separate test for each input can be tedious and repetitive.
Data-driven testing allows you to run the same test logic multiple times, but with different sets of data for each run. This makes your tests more efficient and easier to maintain.
What are Parameterized Tests?
Parameterized tests in JUnit 5 let you write a single test method that can be executed multiple times with different arguments. Instead of copying and pasting test code, you supply the data, and JUnit handles the iterations.
- Saves time and reduces boilerplate code.
- Improves test coverage by easily testing edge cases.
- Makes tests more readable and maintainable.
Using @ParameterizedTest
To create a parameterized test, you use the @ParameterizedTest annotation instead of @Test. You also need to provide a source for the arguments.
JUnit 5 offers several argument sources, like @ValueSource for single arguments or @CsvSource for multiple arguments. Let's look at @ValueSource first.
Simple Data with @ValueSource
The @ValueSource annotation is perfect for providing simple, primitive data types directly in your test. It supports strings, ints, longs, doubles, and more.
Each value in the source will cause the test method to run once with that value as an argument.
@ValueSource in Action
Here's a simple example testing if strings are not null or empty. Notice how the test method accepts a String parameter.
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
class StringValidatorTest {
@ParameterizedTest
@ValueSource(strings = {"apple", "banana", "orange"})
void testStringsAreNotEmpty(String fruit) {
Assertions.assertNotNull(fruit);
Assertions.assertFalse(fruit.isEmpty());
}
}Multiple Args with @CsvSource
When your test method needs more than one argument, @CsvSource comes in handy. It lets you define arguments as comma-separated values (CSV).
Each string in the @CsvSource array represents a row of data, and values are split by commas to match the test method's parameters.
@CsvSource in Action
Let's test a simple addition function. Each line in @CsvSource provides two numbers and their expected sum.
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
class CalculatorTest {
// Simple method to simulate
int add(int a, int b) {
return a + b;
}
@ParameterizedTest
@CsvSource({"1, 1, 2", "2, 3, 5", "5, 0, 5"})
void testAddMethod(int a, int b, int expectedSum) {
Assertions.assertEquals(expectedSum, add(a, b));
}
}Generating Dynamic Tests
Sometimes, the test data or even the number of tests isn't known until runtime. This is where Dynamic Tests shine!
Instead of defining all tests at compile time, dynamic tests allow you to generate them programmatically during test execution. This is useful for complex scenarios or external data sources.
@TestFactory & DynamicTest
To create dynamic tests, you use the @TestFactory annotation. A method annotated with @TestFactory doesn't run a test itself, but rather produces a collection of DynamicTest instances.
@TestFactorymethods must return aStream,Collection,Iterable, orIteratorofDynamicTest.- Each
DynamicTestinstance has a display name and an executable lambda.
Dynamic Test in Action
Here's an example generating tests for string lengths. Each test is created on the fly based on a list of words.
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import java.util.Arrays;
import java.util.Collection;
class DynamicTestExample {
@TestFactory
Collection<DynamicTest> dynamicTestsFromCollection() {
return Arrays.asList(
DynamicTest.dynamicTest("Test 'apple' length",
() -> Assertions.assertEquals(5, "apple".length())),
DynamicTest.dynamicTest("Test 'banana' length",
() -> Assertions.assertEquals(6, "banana".length())),
DynamicTest.dynamicTest("Test 'cat' length",
() -> Assertions.assertEquals(3, "cat".length()))
);
}
}Parameterized vs. Dynamic
Parameterized and Dynamic tests both help reduce boilerplate, but they serve different needs.
Which of the following is the primary benefit of using Parameterized Tests over writing individual @Test methods for similar test cases?
Recap: Flexible Testing
Great job! You've learned how to make your JUnit tests more flexible and efficient:
- Parameterized Tests (
@ParameterizedTestwith sources like@ValueSource,@CsvSource) allow you to run the same test logic with multiple, predefined data sets. - Dynamic Tests (
@TestFactory) let you generate tests programmatically at runtime, ideal for scenarios where test cases are discovered dynamically.
These features help you write cleaner, more comprehensive tests with less effort!
เรียนรู้ Testing Mastery: JUnit, Mockito & Integration Tests ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การทดสอบแบบมีพารามิเตอร์และแบบไดนามิก” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การทดสอบแบบมีพารามิเตอร์และแบบไดนามิก” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Testing Mastery: JUnit, Mockito & Integration Tests ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Testing Mastery: JUnit, Mockito & Integration Tests มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การทดสอบแบบมีพารามิเตอร์และแบบไดนามิก”
เขียนการทดสอบที่มีประสิทธิภาพซึ่งทำงานกับชุดข้อมูลหลายชุด โดยใช้การทดสอบแบบมีพารามิเตอร์และสร้างการทดสอบแบบไดนามิกขณะทำงาน คุณปฏิบัติ Testing Mastery: JUnit, Mockito & Integration Tests ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Testing Mastery: JUnit, Mockito & Integration Tests หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Testing Mastery: JUnit, Mockito & Integration Tests บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การทดสอบแบบมีพารามิเตอร์และแบบไดนามิก” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Testing Mastery: JUnit, Mockito & Integration Tests นี้ได้ไหม
ได้ บทเรียน Testing Mastery: JUnit, Mockito & Integration Tests ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- วงจรชีวิตและลำดับการทดสอบ
- การทดสอบแบบมีพารามิเตอร์และแบบไดนามิก
- การทดสอบข้อยกเว้นและการหมดเวลา
- การทดสอบแบบมีเงื่อนไขและสมมติฐาน