upgrade pyimport to python3
Solution 1:
Python 2.7 is not installed in Ubuntu 20.04 by default, but it can be installed from the terminal. Open the terminal and type:
sudo apt update
sudo apt install --install-suggests python2.7 python-pip python-pytest
python-pytest has been dropped from the default Ubuntu repositories in newer versions of Ubuntu than Ubuntu 20.04.
For Python 3.x:
sudo apt install python3-pip python3-pytest
Example
The first file is the file which should be tested.
fibonacci.py:
def fib(n):
old, new = 0, 1
for _ in range(n):
old, new = new, old + new
return old
This file will be used by 'pytest' to test fibonacci.py:
test_fibonacci.py:
from fibonacci import fib
def test_fib():
assert fib(0) == 0
assert fib(1) == 1
assert fib(10) == 55
Change directories with cd
to the directory that contains fibonacci.py and test_fibonacci.py.
Results of pytest:
============================= test session starts ==============================
platform linux2 -- Python 2.7.17, pytest-3.3.2, py-1.5.2, pluggy-0.6.0
rootdir: /home/karel/Desktop, inifile:
collected 1 item
test_fibonacci.py . [100%]
=========================== 1 passed in 0.04 seconds ===========================
Source of example code: Python Tutorial: Testing with Pytest