Automatically grading CSS
In this page we describe how to automatically test css rules for web development assignments.
Last updated
In this page we describe how to automatically test css rules for web development assignments.
Last updated
While in principle, it is possible to use to test the css
properties defined for html
elements, we advise against doing so in CodeGrade. The reason is that we run Selenium tests by using a web browser in headless mode, which can result in css
rules working differently than they would while running your web browser with a normal display.
Alternatively, we can use to test the structure of the css
rules defined by the student.
Semgrep is used to perform code analysis by writing syntax rules in a human-readable format. Rules are made by generic or language-specific patterns, which are then searched for in the student's code. In Semgrep's , you have:
Equivalences: Matching code that means the same thing even though it looks different;
Wildcards / ellipsis (...): Matching any statement, expression or variable;
Metavariables ($X): Matching unknown expressions that you do not yet know what they will exactly look like, but want to be the same variable in multiple parts of your pattern;
Logical operators: it is possible to logically combine simpler patterns to create more complex aggregate patterns.
Let's inspect an example of a Semgrep pattern that checks the properties of a btn
css
class:
Line 1: with patterns
we indicate that our overall pattern is composed of multiple sub-patterns;
Lines 2 to 3: we require that the patterns that will be defined later have to be found within the body of a class named btn
;
Lines 4 to 7: we require that our pattern contains a margin setting of exactly 20 pixels;
Lines 8 to 11: we require that our pattern contains a width setting with any possible value (notice the use of a metavariable)
In the Setup section of your AutoTest, drag and drop a Script Block into your configuration and type the following bash commands:
In the Tests section of your AutoTest, drag and drop a Code Structure Test block. Specify the name of the CSS file you want to test in the "Student file" field.
Drag and drop a Positive Match block into your configuration and nest it within the Code Structure Test block. Copy and paste your Semgrep rule in the provided editor.
Build and Publish your snapshot.
We suggest using the online to quickly develop new rules for testing css
rules.