I experiment some issues from making tests in another folder than my sample folder:
When created from a different folder, the dataset object haven't the same class.
Explaining: in the test file, I create a dataset object, and launch a parse method who returns a dataset object too. So I have 2 instances of dataset in tests.py, and all of their properties are equals. But when I make a "parseDataset" == "testDataset" in test.py, we reach the first if in the Dataset.eq function because of the 2 objects are not of the same class: 'sample.dataset.Dataset' and 'dataset.Dataset'
Tree:
jojomoon at MacBook-Pro-de-Johann in /tmp/test
$ tree .
.
├── sample
│ ├── dataset.py
│ └── parse.py
└── tests
└── tests.py
2 directories, 3 files
dataset.py
import sys
import strings
class Dataset(object):
def __init__(self, dataTab):
self.nbFeat = len(dataTab[0])
self.nbData = len(dataTab)
# ... other cool stuff but doesn't care here
def __eq__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return self.__dict__ == other.__dict__
parse.py
def dataset(fileName):
#... parse the file, and create an array
return dataset.Dataset(array)
# the array is the same as the one in tests.py
tests.py
import unittest
import os
# Get current path
current = os.path.dirname(os.path.abspath(__file__))
# Get parent path
parent = os.path.dirname(current)
# Add parent to python paths
sys.path.append(parent)
# Add sample to python paths
sys.path.append(parent + "/sample")
from sample import parse, dataset
class TestStringMethods(unittest.TestCase):
def test_parse_Unit_normal(self):
# ... opening a file, writing to it an example dataset.
# now launching a parse on it:
outputDS = parse.dataSet("./data/test_parse_Unit_normal.csv")
# create the expected dataset
expectTab = [
["Data1","Data2","Data3","Data4"],
["1","2","3","4"],
["5","6","7","8"]
]
expectDS = dataset.Dataset(expectTab)
# here outputDS.__class__ = <class 'sample.dataset.Dataset'>
# and expextDS.__class__ = <class 'dataset.Dataset'>
# so in the Dataset.__eq__() function, isinstance(other.__class__, self.__class__) = False
self.assertTrue(outputDS == expectDS)
How can I resolve this problem? Yes I can compare value by value in tests.py or delete the instance condition in dataset class but it's just avoiding the problem, not resolving it.
-EDIT- I understand that the problem is about my imports in tests.py. How, in tests.py can I import my sample module? relative path: ../sample
-EDIT2- Okay, I tested with the following:
tests.py
from ..sample import dataset
and get this error:
Traceback (most recent call last):
File "tests/tests.py", line 20, in <module>
from ..sample import dataset
ValueError: attempted relative import beyond top-level package
So with searches I found explanations, but no other solutions that will make my problem of type disappear
-EDIT3- SO when (in my example) I go to the parent directory of my project named dslr, and launch with a python3.7 -m dslr.tests.tests, the following line works: from dslr import sample
. It's cool, it's working, but it's messy. I have to change all relative paths in my project. If no one has the solution I'll do it, because I'm running out of patience.