Static Code Analysis: What It Is + How to Use It in Katalon

static code analysis with katalon

Static code analysis examines code without running it to catch errors, standards violations, and security issues. Learn why it matters for test automation code and how to run CodeNarc on your Katalon Studio scripts with Gradle.

Key takeaways
The 30-second version of this article
Static code analysis checks code for errors, standards violations, undefined values, and security issues without running it.
Applied to test code, it makes scripts faster, more readable, and easier to debug.
CodeNarc is a static analysis tool for Groovy, the scripting language used in Katalon Studio.
Add CodeNarc to your project's build.gradle, provide a ruleset file, and run the Gradle task to get an HTML report.

In today's fast-paced software development environment, ensuring the quality and security of code is paramount. Static Code Analysis has emerged as a vital practice in this realm, offering a systematic method to examine code without the need to execute it. In this blog, you will delve into the essence of Static Code Analysis, its applicability, and the significant benefits it offers, particularly in the context of testing code. You will also be guided through performing Static Code Analysis on test scripts using CodeNarc in Katalon Studio.

What is static code analysis?

Static code analysis is a method of examining the written software code without executing the program. The result of the examination provides an understanding of the code structure, coding practices, vulnerabilities, and adherence to industry standards.

Static analysis covers the following during examination:

  • Programming errors
  • Coding standard violations
  • Undefined values
  • Syntax violations
  • Security vulnerabilities
     

Who does static code analysis?

Static code analysis has been standard practice in software engineering for decades, and QA teams are now adopting it to screen their test automation code too.

Why should test code be screened with static code analysis?

There are a couple of benefits that Static Code Analysis can provide for testing code:

  • More performant scripts, because engineers are pushed to fix inefficient code, which speeds up automation and reduces memory and processing use.
  • More readable code when it’s reviewed, maintained, or reused by someone other than the author.
  • Easier, faster debugging, with unused statements flagged for removal.

How to perform static code analysis on test code

Generally, Static Code Analysis is performed by a variety of tools including open-source options. The options vary based on the language used and the type of analysis on which the emphasis would be.
 

Since Katalon Studio uses Groovy as its scripting language, several proprietary and open-source tools are compatible with it. Here, you will use CodeNarc, a static analysis tool for the Groovy language.

Here are the steps:

  1. Create a project with the Generate build.gradle file option checked.
  1. In the project folder, open the build.gradle file and add the CodeNarc dependency and task. The complete file is below:
plugins {
    id 'java'
    id 'groovy'
    id 'com.katalon.gradle-plugin' version '0.1.1'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.codenarc:CodeNarc:3.4.0'
}

tasks.register('runCodeNarc', JavaExec) {
    mainClass = 'org.codenarc.CodeNarc'
    classpath = sourceSets.main.runtimeClasspath
    args '-rulesetfiles=file:codenarc.ruleset'
    args '-includes=Scripts/**/*.groovy,Keywords/**/*.groovy'
}

3. Go to the CodeNarc website to download the available ruleset from the link here. Once downloaded rename the file to “codenarc.ruleset” and move it to the Katalon project folder where you have build.gradle file.

Sample ruleset file:

image1.png
  1. Open a terminal and cd to the Katalon project folder. Then run gradle runCodeNarc (replace runCodeNarc with the task name you defined). Once the job runs successfully, you should find CodeNarcReport.html in the project folder, along with build.gradle and codenarc.ruleset.

Sample report:

  1. Visit the CodeNarc website to learn more about custom rules and rulesets.

Conclusion

Static code analysis is an important practice in modern software development, improving code quality, performance, and security. By adding tools like CodeNarc to your workflow, you can keep your test automation scripts performant, maintainable, and secure, which leads to more reliable software and smoother development and QA processes.

Frequently asked questions

Thong Tran
Thong TranSenior Software Engineer

Thong Tran is a Senior Software Engineer at Katalon, where he helps build Katalon Studio. He brings a people-first mindset to engineering and focuses on creating real value for users rather than chasing tech trends. Beyond his engineering work, he regularly shares product knowledge with colleagues and supports the Sales team.

Get started

Test everything. Ship with confidence.

From generating test cases to spotting failures, Katalon's AI works across your entire pipeline, automatically.

Live webinar

The Next Evolution of Manual Testing: Introducing Katalon AI Test Runner

Learn how to grow your QA team's capacity in the age of AI, then watch Katalon AI Test Runner turn manual test cases into automated runs, live.

2:00 to 3:00 PM IST
Almas AhmedSenior Account Manager
Sumit WankhedeSolutions Engineer Manager
Register now