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 "6.2.5", pytest-testdox and mock. You can install them by running the following command: pip3 install pytest==6.2.5 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:
1# test.py 2 3@pytest.mark.it('You should create a variable named variables_are_cool') 4 5def test_variable_exists(app): 6 try: 7 app.variables_are_cool 8 except AttributeError: 9 raise AttributeError('The variable "variables_are_cool" should exist on app.py')
1# test.py 2 3@pytest.mark.it('You should create a variable named myVariable with the value "Hello"') 4def test_variable_exists(app): 5 try: 6 assert app.myVariable == "Hello" 7 except AttributeError: 8 raise AttributeError('The variable "myVariable" should exists')
1# test.py 2 3@pytest.mark.it('You should create a function named "myFunction"') 4def test_variable_exists(app): 5 try: 6 assert app.myFunction 7 except AttributeError: 8 raise AttributeError('The function "myFunction" should exists')
1# test.py 2 3@pytest.mark.it('The function "myFunction" should return the value "Hello World!"') 4def test_variable_exists(app): 5 try: 6 assert app.myFunction == "Hello World!" 7 except AttributeError: 8 raise AttributeError('The function "myFunction" should exists')
1# test.py 2 3import os, re 4 5@pytest.mark.it("Create a variable named 'color' with the string value red") 6def test_declare_variable(): 7 path = os.path.dirname(os.path.abspath(__file__))+'/app.py' 8 with open(path, 'r') as content_file: 9 content = content_file.read() 10 regex = re.compile(r"color(\s*)=(\s*)\"red\"") 11 assert bool(regex.search(content)) == True
1# test.py 2 3import io, sys 4 5@pytest.mark.it('The printed value on the console should be "red"') 6def test_for_file_output(capsys, app): 7 app() 8 captured = capsys.readouterr() 9 assert "red\n" == captured.out
The following links are Python coding tutorials: