ความครอบคลุมโค้ดและการวิเคราะห์แบบสถิต
ผสานเครื่องมือวัดความครอบคลุมโค้ด เช่น JaCoCo และเครื่องมือวิเคราะห์แบบสถิต เช่น Checkstyle/PMD เข้ากับการสร้างด้วย Gradle
ความครอบคลุมโค้ดและการวิเคราะห์แบบสถิต เป็นบทเรียน Groovy & Gradle: JVM Automation and Build Engineering ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Groovy & Gradle: JVM Automation and Build Engineering และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Groovy & Gradle: JVM Automation and Build Engineering มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Welcome to Code Quality
Ensuring high-quality code is crucial for reliable software. In this lesson, we'll explore two key practices:
- Code Coverage: How much of your code is tested?
- Static Analysis: Does your code follow best practices and style guidelines?
These tools help you write better, more maintainable code by identifying issues early.
Understanding Code Coverage
Code coverage measures the percentage of your source code that is executed by your tests. It's a metric that tells you how thoroughly your tests are exercising your application's logic.
While high coverage doesn't guarantee bug-free code, it indicates a good foundation of testing and helps pinpoint untested areas.
Meet JaCoCo for Coverage
JaCoCo (Java Code Coverage) is a popular and free code coverage library for Java projects, which works seamlessly with Groovy. It integrates exceptionally well with build tools like Gradle.
JaCoCo generates detailed reports showing which parts of your code are covered by tests and which are not, helping you identify gaps.
JaCoCo Plugin & Basic Config
To integrate JaCoCo, apply its plugin in your build.gradle. This automatically adds tasks and allows you to configure report generation, specifying formats like HTML and XML.
We'll also include basic Java/JUnit setup for context.
plugins {
id 'java'
id 'jacoco'
}
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.0'
}
test {
useJUnitPlatform()
}
jacoco {
toolVersion = "0.8.11" // Use a recent version
}
jacocoTestReport {
reports {
xml.required = true
html.required = true
csv.required = false // Optional: disable CSV
}
}Executing JaCoCo Tasks
Once configured in build.gradle, you can run the JaCoCo test report task from your terminal. This command will execute your tests and then generate the coverage report.
The interactive HTML report can be found in build/reports/jacoco/test/html/index.html.
// In your terminal:
// gradlew clean test jacocoTestReportUnderstanding Static Analysis
Static analysis is the examination of your code without actually executing it. It's like a linter or a spell checker for your code, identifying potential issues before runtime.
It helps identify problems such as:
- Coding standard violations
- Potential bugs (e.g., null pointer dereferences)
- Security vulnerabilities
Meet Checkstyle for Style
Checkstyle is a development tool that helps programmers write Java (and often Groovy) code that adheres to a coding standard. It automates the process of checking code against a predefined set of rules.
By catching style and common error violations early, it improves code consistency and readability.
Checkstyle Plugin & Basic Config
To integrate Checkstyle, apply its plugin in your build.gradle. You'll typically point it to a ruleset file, often located within your project's config/checkstyle directory, or use a default.
This configuration also ensures reports are generated in useful formats like HTML and XML.
// build.gradle
plugins {
id 'java'
id 'jacoco'
id 'checkstyle' // Add this line
}
// ... other existing configurations like repositories, dependencies, test block ...
checkstyle {
toolVersion = "10.12.7" // Use a recent version
// Point to your custom ruleset file (e.g., a Google style guide)
configFile = file("${project.rootDir}/config/checkstyle/checkstyle.xml")
}
tasks.withType(Checkstyle) {
reports {
xml.required = true
html.required = true
}
}Executing Checkstyle Tasks
After configuration, you can run Checkstyle from your terminal. It will analyze your source code and generate reports without compiling or running tests.
You can find the HTML report in build/reports/checkstyle/main.html for your main source code.
// In your terminal:
// gradlew checkstyleMainQuick Check on Quality Tools
You've learned about two powerful tools for code quality. Let's test your understanding.
Recap: Quality Assurance
In this lesson, you learned how to integrate essential quality assurance tools into your Gradle build:
- JaCoCo: For measuring code coverage, showing how much of your code is exercised by tests.
- Checkstyle: For static analysis, enforcing coding standards and identifying potential issues without running the code.
These tools are vital for maintaining high-quality, readable, and maintainable software.
คำถามที่พบบ่อย
บทเรียน “ความครอบคลุมโค้ดและการวิเคราะห์แบบสถิต” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ความครอบคลุมโค้ดและการวิเคราะห์แบบสถิต” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Groovy & Gradle: JVM Automation and Build Engineering ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Groovy & Gradle: JVM Automation and Build Engineering มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ความครอบคลุมโค้ดและการวิเคราะห์แบบสถิต”
ผสานเครื่องมือวัดความครอบคลุมโค้ด เช่น JaCoCo และเครื่องมือวิเคราะห์แบบสถิต เช่น Checkstyle/PMD เข้ากับการสร้างด้วย Gradle คุณปฏิบัติ Groovy & Gradle: JVM Automation and Build Engineering ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Groovy & Gradle: JVM Automation and Build Engineering หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Groovy & Gradle: JVM Automation and Build Engineering บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “ความครอบคลุมโค้ดและการวิเคราะห์แบบสถิต” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Groovy & Gradle: JVM Automation and Build Engineering นี้ได้ไหม
ได้ บทเรียน Groovy & Gradle: JVM Automation and Build Engineering ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การทดสอบหน่วยและการทดสอบการเชื่อมต่อ
- รายงานการทดสอบและการกรอง
- ความครอบคลุมโค้ดและการวิเคราะห์แบบสถิต
- ชุดทดสอบและโค้ดทดสอบร่วม