I have folder filled with unit tests. I am importing code from another folder that I am testing. I am almost sure that it is not because of the modules that I am importing, but just incase I included them at the bottom. Any tips or insight would be greatly appreciated. So one of them runs when I use
python3 -m unittest tests/test_models/test_base.py
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
However, the other unittest file wont run any tests with the same command
python3 -m unittest tests/test_models/test_rectangle.py
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
Here is my test_base.py file
#!/usr/bin/python3
"""Unit test for base.py"""
import unittest
from models.base import Base
class TestMethods(unittest.TestCase):
"""Defining test methods class"""
def test_with_num(self):
"""Test to see if base method works with input"""
b1 = Base(12)
self.assertEqual(b1.id, 12)
def test_without_num(self):
"""Test to see id without input"""
b2 = Base()
self.assertEqual(b2.id, 1)
if __name__ == "__main__":
unittest.main()
And here is my test_rectange.py
#!/usr/bin/python3
"""This file is unit testing for rectange class"""
import unittest
from models.rectangle import Rectangle
class TestMethods(unittest.TestCase):
"""Defining test methods class"""
def width_test(self):
"""Normal Rectangle Test"""
r1 = Rectangle(2, 3, 0, 0, 1)
self.assertEqual(r1.width, 2)
if __name__ == "__main__":
unittest.main()
Here are the modules that I am importing base.py
#!/usr/bin/python3
"""Task 1"""
import json
import copy
class Base:
"""Defines class Base"""
__nb_objects = 0
def __init__(self, id=None):
"""Initialization method"""
if id is not None:
self.id = id
else:
Base.__nb_objects += 1
self.id = Base.__nb_objects
@staticmethod
def to_json_string(list_dictionaries):
"""Returns a string reresentation of obj"""
return json.dumps(list_dictionaries)
@classmethod
def save_to_file(cls, list_objs):
"""Writes json representation of string"""
dictionaries = []
if list_objs is not None:
for thing in list_objs:
dictionaries.append(thing.to_dictionary())
with open(cls.__name__ + ".json", "w+") as f:
f.write(Base.to_json_string(dictionaries))
f.close()
@staticmethod
def from_json_string(json_string):
"""Returns a list of dictionaries from a string"""
return list(eval(json_string))
@classmethod
def create(cls, **dictionary):
"""Returns an instance with all attributes already set"""
if dictionary is None:
return None
if cls.__name__ == "Rectangle":
new = cls(1, 1)
else:
new = cls(1)
new.update(**dictionary)
return new
@classmethod
def load_from_file(cls):
"""Reads a file and returns a list of instances"""
list_instances = []
try:
with open(cls.__name__ + ".json", "r") as f:
data = f.read()
dicts = Base.from_json_string(data)
for entry in dicts:
list_instances.append(cls.create(**entry))
except FileNotFoundError:
pass
return list_instances
And here is rectangle.py
#!/usr/bin/python3
"""This file dfine sthe rectangle class"""
from models.base import Base
class Rectangle(Base):
"""Defines Rectangle Class"""
def __init__(self, width, height, x=0, y=0, id=None):
"""Initialization method"""
super().__init__(id)
self.width = width
self.height = height
self.x = x
self.y = y
@property
def width(self):
"""Gets width"""
return self.__width
@width.setter
def width(self, val):
"""Sets width"""
if type(val) is not int:
raise TypeError("width must be an integer")
elif val <= 0:
raise ValueError("width must be > 0")
else:
self.__width = val
@property
def height(self):
"""Gets height"""
return self.__height
@height.setter
def height(self, val):
"""Sets height"""
if type(val) is not int:
raise TypeError("height must be an integer")
elif val<= 0:
raise ValueError("height must be > 0")
else:
self.__height = val
@property
def x(self):
"""Gets x"""
return self.__x
@x.setter
def x(self, val):
"""sets x"""
if type(val) is not int:
raise TypeError("x must be an integer")
elif val < 0:
raise ValueError("x must be >= 0")
else:
self.__x = val
@property
def y(self):
"""Gets y"""
return self.__y
@y.setter
def y(self, val):
"""Sets y"""
if type(val) is not int:
raise TypeError("y must be an integer")
elif val < 0:
raise ValueError("y must be >= 0")
else:
self.__y = val
def area(self):
"""Public method that returns the area of a rectangle"""
return self.width * self.height
def display(self):
"""Displays the rectangle"""
for i in range(0, self.y):
print("")
for j in range(0, self.height):
for k in range(0, self.x):
print(" ", end="")
for l in range(0, self.width):
print("#", end="")
print("")
def __str__(self):
"""Returns string with specific format for obj"""
return "[Rectangle] ({:d}) {:d}/{:d} - {:d}/{:d}".format(self.id, self.x, self.y, self.width, self.height)
def update(self, *args, **kwargs):
"""Assigns an argument each attribute"""
attrs = ["id", "width", "height", "x", "y"]
if args is not None:
for i in range(0, len(args)):
setattr(self, attrs[i], args[i])
if kwargs is not None:
for key, value in kwargs.items():
setattr(self, key, value)
def to_dictionary(self):
return {'x': self.x, 'y': self.y, 'id': self.id, 'height': self.height, 'width': self.width}
107,1 Bot
question from:
https://stackoverflow.com/questions/65908267/one-of-my-unit-tests-runs-while-the-other-does-not