Python
If you want to create a Python coding tutorial, you have to use the
@learnpack/python
plugin in order to compile and test your exercises. Go to Compiler Plugins to see how to install the plugin.In addition, the main file should be named
app.py
, and the file for the tests, should be named test.py
.For testing, you should install pytest with the version "4.6.0", pytest-testdox and mock. You can install them by running the following command:
pip3 install pytest==4.6.0 pytest-testdox mock
If you are not so familiar with
Pytest
, here are the most common tests that you will use when testing a Python exercise tutorial:test.py
@pytest.mark.it('You should create a variable named variables_are_cool')
def test_variable_exists(app):
try:
app.variables_are_cool
except AttributeError:
raise AttributeError('The variable "variables_are_cool" should exist on app.py')
test.py
@pytest.mark.it('You should create a variable named myVariable with the value "Hello"')
def test_variable_exists(app):
try:
assert app.myVariable == "Hello"
except AttributeError:
raise AttributeError('The variable "myVariable" should exists')
test.py
@pytest.mark.it('You should create a function named "myFunction"')
def test_variable_exists(app):
try:
assert app.myFunction
except AttributeError:
raise AttributeError('The function "myFunction" should exists')
test.py
@pytest.mark.it('The function "myFunction" should return the value "Hello World!"')
def test_variable_exists(app):
try:
assert app.myFunction == "Hello World!"
except AttributeError:
raise AttributeError('The function "myFunction" should exists')
test.py
import os, re
@pytest.mark.it("Create a variable named 'color' with the string value red")
def test_declare_variable():
path = os.path.dirname(os.path.abspath(__file__))+'/app.py'
with open(path, 'r') as content_file:
content = content_file.read()
regex = re.compile(r"color(\s*)=(\s*)\"red\"")
assert bool(regex.search(content)) == True
test.py
import io, sys
@pytest.mark.it('The printed value on the console should be "red"')
def test_for_file_output(capsys, app):
app()
captured = capsys.readouterr()
assert "red\n" == captured.out
The following links are python coding tutorials:
Last modified 10mo ago