I need to use unittest
python library to execute tests about the 3 functions in src/arithmetics.py
file. Here is my project structure.
.
├── src
│ └── arithmetics.py
└── test
└── lcm
├── __init__.py
├── test_lcm_exception.py
└── test_lcm.py
src/arithmetics.py
def lcm(p, q):
p, q = abs(p), abs(q)
m = p * q
while True:
p %= q
if not p:
return m // q
q %= p
if not q:
return m // p
def lcm_better(p, q):
p, q = abs(p), abs(q)
m = p * q
h = p % q
while h != 0:
p = q
q = h
h = p % q
h = m / q
return h
def lcm_faulty(p, q):
r, m = 0, 0
r = p * q
while (r > p) and (r > q):
if (r % p == 0) and (r % q == 0):
m = r
r = r - 1
return m
test/lcm/test_lcm.py
import unittest
from src.arithmetics import *
class LcmTest(unittest.TestCase):
def test_lcm(self):
for X in range(1, 100):
self.assertTrue(0 == lcm(0, X))
self.assertTrue(X == lcm(X, X))
self.assertTrue(840 == lcm(60, 168))
def test_lcm_better(self):
for X in range(1, 100):
self.assertTrue(0 == lcm_better(0, X))
self.assertTrue(X == lcm_better(X, X))
self.assertTrue(840 == lcm_better(60, 168))
def test_lcm_faulty(self):
self.assertTrue(0 == lcm_faulty(0, 0))
for X in range(1, 100):
self.assertTrue(0 == lcm_faulty(X, 0))
self.assertTrue(0 == lcm_faulty(0, X))
self.assertTrue(840 == lcm_faulty(60, 168))
if __name__ == '__main__':
unittest.main()
test/lcm/test_lcm_exception.py
import unittest
from src.arithmetics import *
class LcmExceptionTest(unittest.TestCase):
def test_lcm_exception(self):
for X in range(0, 100):
self.assertTrue(0 == lcm(0, 0)) # ZeroDivisionError
self.assertTrue(0 == lcm(X, 0)) # ZeroDivisionError
def test_lcm_better_exception(self):
for X in range(0, 100):
self.assertTrue(0 == lcm_better(0, 0)) # ZeroDivisionError
self.assertTrue(0 == lcm_better(X, 0)) # ZeroDivisionError
def test_lcm_faulty_exception(self):
for X in range(1, 100):
self.assertTrue(X == lcm_faulty(X, X)) # ppcm(1, 1) != 1
if __name__ == '__main__':
unittest.main()
test/lcm/__init__.py
is an empty file
To execute my tests, I tried this command :
python3 -m unittest discover
But the output is :
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
I don't understand how can I run my tests...
Thanks for helping me !
CodePudding user response:
Some files init.py are missing
I think that the problem is the missing of file __init__.py
in your subfolders. Try to add this empty file in all your subfolders as I show you below:
test_lcm
├── __init__.py
├── src
| └── __init__py
│ └── arithmetics.py
└── test
└── __init__py
└── lcm
├── __init__.py
├── test_lcm_exception.py
└── test_lcm.py
If you see my tree folder I have create a folder test_lcm
as root of the tree. You have to place inside it by cd
command.
So in test_lcm
folder execute:
# go to test_lcm folder
cd ~/test_lcm
# execute test
python3 -m unittest discover
The last part of the output is:
----------------------------------------------------------------------
Ran 6 tests in 0.002s
FAILED (failures=1, errors=2)
This show that are executed 6 tests with 2 errors (test_lcm_better_exception
and test_lcm_exception
fail).