Coverage of dynamically called code?

Hello, for our Python code base, we are using pytest to write tests. We want to insure that Sonarqube reports coverage above 75%, but for certain classes, we dont directly instantiate the classes or call their methods.

Instead, based on reading an input file, we compute which class to instantiate and then call a method in that class.

So if code coverage in Sonarqube does a static analysis of the tests to see what they are calling, then it will not see any code instantiating instances of those classes.

On the other hand, if Sonarqube detects what code paths were executed when the test ran, after the test did run, then it will notice that a certain code path was covered by the test based on its response to the input file.

Hello @metaperl, welcome to the Community!

It doesn’t sound like you yet have test coverage in place for these projects involving SonarQube (correct me if I’m wrong). If that’s correct, you may not yet know that SonarQube relies on importing a coverage report for the project. The mechanism to report coverage by default is language-specific and documented here. Also note that if you don’t have a cobertura-format XML coverage report for your python code, it may also be possible for you to provide a report in our generic format.

Bottom line: it’s up to you to provide a coverage report, from which we’ll then determine how much of the source is covered by tests. Which means it’s also up to you to make sure the execution involving dynamic instantiation is included in that report.

Hope this helps clarify!