E
E
ekolodenets2020-07-05 12:22:36
Python
ekolodenets, 2020-07-05 12:22:36

Python. What is the problem with the test?

the end of the file for the test that translates the entered line into a dictionary list and the number of all atoms:

if __name__ == '__main__':
    var = 'H2O'
    result = MyClass().parse(var)
    print(result)


test content:
import unittest
from ..Tasks.MoleculeToAtoms import MyClass

class Test_parse(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_parsing(self):
        self.assertEqual(MyClass().parse('H2O'), ({'H': 2, 'O': 1}))
        self.assertEqual(MyClass().parse('Mg(OH)2'), ({'Mg': 1, 'O': 2, 'H': 2}))
        self.assertEqual(MyClass().parse('K4[ON(SO3)2]2'), ({'K': 4, 'O': 14, 'N': 2, 'S': 4}))


everything seemed to work out. The test passed.. added the 4th line a problem started that is not solved even by deleting it
Error: ValueError: attempted relative import beyond top-level package

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Link, 2020-07-05
@DmitryLink

Hello. You need to create a setup.py file at the root of your project, here is the documentation: https://docs.python.org/3/distutils/setupscript.html
After that, install your package - pip install --editable . (dot at the end). Further in the tests, you can import the package like this:
from tasks import MyClass

E
ekolodenets, 2020-07-05
@ekolodenets

kapets .. removing some 2 points solved the issue. but it's still not clear how Python knows that the desired folder is one level higher?

import unittest

from Tasks.MoleculeToAtoms import MyClass

class Test_parse(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_parsing(self):

        self.assertEqual(MyClass().parse('H2O'), ({'H': 2, 'O': 1}))
        self.assertEqual(MyClass().parse('Mg(OH)2'), ({'Mg': 1, 'O': 2, 'H': 2}))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question