Code structure test

Often the learning goals of programming assignments amount to whether students are able to understand and apply specific methods, structures or strategies in solving coding problems. While it's possible through unit tests to check whether functions have been defined correctly and whether they return the correct result, it is still implicit rather then explicit. Semgrep is code structure testing framework that uses semantic expression matching (similar to regex) to match blocks of code in a script. Semgrep support extends to almost all major programming languages. The Code structure test block allows you to effortlessly run Semgrep tests in CodeGrade.

the Code structure test block comes with an option of two match blocks:

  • Positive match - Check that the desired code structure is present in the selected file.

  • Negative match - Check that the specified code structure is not present in the selected file.

There is often some trial and error involved in crafting Semgrep patterns. It's highly recommended that you first craft your tests in the Semgrep editor which you can find here:

How to use the Code structure test block

  1. Navigate to the tests portion of the AutoTest settings.

  2. Click the Code structure test block to add it to the bottom of your configuration or drag-and-drop it to the desired location.

  3. Select the version of Semgrep you would like to use with the Install version drop-down menu.

  4. Provide the name of the file you would like to test in the Student file input field

  5. Add your desired match block your configuration and nest it within the Code structure test block.

  6. Navigate to Semgrep's editor and craft your pattern. Once you're satisfied, switch to "advanced" mode and copy and paste the entire semgrep rule into the match block.

Last updated