5.3 Hello World in Python

1cd ~/python 
2poetry run pytest

The poetry run pytest command runs a set of tests to confirm that the requirements have been met (these tests are often called ‘acceptance tests’). Later in this course we will look at these tests and how, when, and why to write our own tests.

poetry run pytest
1Creating virtualenv convert-3AT6xhsc-py3.8 in /home/vagrant/.cache/pypoetry/virtualenvs 
2============================== test session starts =============================== 
3platform linux -- Python 3.9.2, pytest-7.1.2, pluggy-1.0.0 
4rootdir: /home/vagrant/python 
5plugins: bdd-6.0.1 
6collected 1 item 
7 
8tests/test_convert.py F                                                                                                                                                                                                                                                                                                                          [100%] 
9 
10==================================== FAILURES ==================================== 
11 test_welcome_message ____________________________ 
12 
13capfd = <_pytest.capture.CaptureFixture object at 0x7f80b8694e80> 
14 
15    def test_welcome_message(capfd): 
16>       import convert 
17E       ModuleNotFoundError: No module named 'convert' 
18 
19tests/test_convert.py:3: ModuleNotFoundError 
20============================= short test summary info =========================== 
21FAILED tests/test_convert.py::test_welcome_message - ModuleNotFoundError: No module named 'convert' 
22=============================== 1 failed in 0.10s ===============================

The poetry run pytest tests are currently failing, which should be unsurprising as we have not yet written any code. The output from poetry run pytest may be confusing but don’t worry too much about it at the moment, the main things to notice are on line 22 (telling us we had one failure) and line 21 (telling us that the problem is that the convert module does not yet exist).


In that line 21 message we see that the error is in test_welcome_message, which gives us a clue as to what we are expected to provide.

Let’s now write the code to satisfy our requirement.

1vi convert.py

Add the following, single line, to the convert.py file and save the file.

convert.py
1print("Hello World!")

Running poetry run python convert.py will print the message ‘Hello World!’ followed by a new line to the screen2.

1poetry run python convert.py
poetry run python convert.py
1Hello World!

If we now run our tests they pass.

1poetry run pytest
poetry run pytest
1============================== test session starts =============================== 
2platform linux -- Python 3.9.2, pytest-7.1.2, pluggy-1.0.0 
3rootdir: /home/vagrant/python 
4plugins: bdd-6.0.1 
5collected 1 item 
6 
7tests/test_convert.py .                                                                                                                                                                                                                                                                                                                          [100%] 
8 
9=============================== 1 passed in 0.03s ====================================================

The only interesting line in this output is line 9 where we are told we ran one passing test.

Congratulations! You have written a simple python script that outputs a message to the screen.

2In general we can run Python scripts using python convert.py. We use poetry run which runs our script via poetry and this takes care of managing the python environment. DON’T PANIC! All of this will be explained later.