🏗️Code Structure Tests with Semgrep
Learn how to create effective tests to check that your students employ the required coding patterns.
Semgrep is a static code analysis tool that can check if students have used a specific syntax or coding pattern to reach their solution. AutoTest offers the Code structure test block and its corresponding Positive match and Negative match blocks for running Semgrep tests. These blocks are configured by specifying Semgrep patterns.
To get familiar with Semgrep patterns, we highly recommend the following resources:
Semgrep's online documentation has many interactive tutorials and examples.
Semgrep's online playground is helpful for quickly developing and debugging your patterns.
This page is divided into two parts. The first part walks you through creating a Code Structure Test in AutoTest. The second part is a collection of Semgrep patterns that you can use out of the box for your tests.
Setting up a Code Structure Test in AutoTest
Follow these steps in the AutoTest Editor to create a Code Structure Test:
Add a Code Structure Test Block to your AutoTest configuration.
In the Student file input field, write the name of the file you want to check. The default value will check every file in the Student Directory.
Add an inner Positive or Negative Match Block within the Code Structure Test Block. The Positive Match will pass if the expected coding pattern is found in the target file, while for the Negative Match, a test passes if the expected coding pattern is not found.
Edit the template configuration of the Match Block you want to configure.

Tips and tricks
Multiline patterns: whenever your pattern consists of more than one line, use the vertical bar | to indicate a multiline pattern, like in the following Python example:
Metavariables: metavariables are capital case variables starting with $, like
$Nor$VAR. They work as placeholders for actual variables, expressions, or values. They are helpful to write flexible and versatile patterns so that the student is not required to match a piece of code literally.Ellipsis operator
...: the...ellipsis operator abstracts away a sequence of zero or more items such as arguments, statements, parameters, fields, and characters.Literal patterns: all the parts in your pattern that do not contain metavariables or the
...operator must be exactly matched by the student's code. Mixing literal and abstract fragments offers a great deal of versatility for devising patterns that can be both as granular and as flexible as desired.
Logical operators for rules:
Semgrep allows the creation of composite patterns by applying logical operators on sub-patterns. In CodeGrade, the most commonly used operators are:
pattern: a simple single pattern (possibly multiline).patterns: corresponding to the AND logical operator. Other sub-patterns can be nested within it so that the overall pattern will be matched if and only if all the sub-patterns are matched.
pattern-either: corresponding to the OR logical operator. Other sub-patterns can be nested within it so that the overall pattern will be matched if at least one sub-pattern is matched.
pattern-inside: Thepattern-insideoperator keeps matched findings that reside within its expression. This is useful for finding code inside other pieces of code, such as functions or blocks.
Semgrep Pattern Collection
While our automatic grading guides include examples of Semgrep patterns, we provide a more extensive collection of patterns for the most common programming languages and use-case scenarios here. If you have further questions, don't hesitate to contact us at support@codegrade.com!
Last updated