Self-paced

Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.

Bootcamp

Learn live

Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.

Upcoming live events

Learning library

For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Search from all Lessons


LoginGet Started
← Back to Lessons
Edit on Github

Building Python Tutorials with LearnPack

Examples

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.

Unit Testing

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:

Testing if a variable exists

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')

Testing a variable has the expected value

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')

Testing a function 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')

Testing a function returns the expected value

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')

Testing the file with regular expressions:

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

Testing a particular string has been printed in the console

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

Examples