Testing Example Plugin - 2024

I’m developing rules for a non-supported language (Gherkin) using sonar-custom-plugin-example shown in this doc

I need help to develop an unit test layer to assert that my rules are working correctly. However, there is no template test layer for new language rules (like Foo), I tried creating one manually, but I don’t seem to find a way to run this program without fully packaging and installing it within a SonarQube instance.

Problem
I don’t seem to find a way to instantiate a class that implements SensorContext (org\sonar\api\batch\sensor\SensorContext.class) to use in my test layer

If possible, I’d like to see an template Unit Test using the Foo Language in this Example Plugin

I’ve been searching around and found that sonar-java has an implementation regarding testing, is it possible to replicate this behaviour in my plugin?

Cheers,
André

Hello @balbi_andre,

Thanks for raising your hand.

It’s not completely clear to me. Do you want to implement custom rules for Java language or do you want to implement a completely new plugin to analyze a new language?

If you want to write custom Java rules then you indeed need to use this guide.

However, if you want to implement a completely new language plugin then you will need to use this dependency.

Let me know if that answers your question.

Best,
Margarita

2 Likes

Hello @Margarita_Nedzelska, first of all, thanks for responding.

I believe the dependency you sent would not satisfy my needs.

My objective:
On a Higher level: I want to use the example plugin as basis to integrate a non-supported Language and develop new rules.
On a Lower level: I will use a Gherkin parser to analyse Karate DSL code in .feature files. Issues found will be reported and shown in SonarQube.

My plan
I want to fork this Example Plugin into a customized plugin where I can write my implementation.

Problem
I can’t find a way to test this same plugin, there is no way to instantiate SensorContext to extract data from the file.

Is there any way to test rule implementation in this specific plugin, or should I explore another way? TDD is a priority for me.

Thanks!

@balbi_andre since now this is a bit more clear what you’re trying to achieve. So the dependency I shared with you is more to write unit tests and check the rules without the context. So You will need to create your verifier and create a mocked test context.

If you want to actually check the integration there’s a testkit for SQ called orchestrator: GitHub - SonarSource/orchestrator: Java library for running SonarQube in tests It is used for heavy integration tests.

For unit tests of the plugin or sensors, you can check out this dependency: GitHub - SonarSource/sonar-plugin-api: API to develop plugins for SonarQube, SonarCloud and SonarLint. For example for having test sensor context you can use SensorContextTester as mentioned here.

Best,
Margarita

2 Likes