使用Python分离src和test目录

使用Python分离src和test目录,python,unit-testing,class,visual-studio-code,python-import,Python,Unit Testing,Class,Visual Studio Code,Python Import,当我将测试和src文件放在不同的子文件夹中时,我在测试文件的from行上遇到编译错误:“无法导入'classA'pylint(导入错误)”。当src和测试文件在同一个目录下时,它就工作了。这让我觉得这与settings.json文件有关,但我不确定。关于如何修复它,有什么想法吗 repo ├── .vscode │ └── settings.json ├── src │ ├── classA.py │ └── classB.py └── tests ├── classA_t

当我将测试和src文件放在不同的子文件夹中时,我在测试文件的
from
行上遇到编译错误:
“无法导入'classA'pylint(导入错误)”
。当src和测试文件在同一个目录下时,它就工作了。这让我觉得这与settings.json文件有关,但我不确定。关于如何修复它,有什么想法吗

repo
├── .vscode
│   └── settings.json
├── src
│   ├── classA.py
│   └── classB.py
└── tests
     ├── classA_test.py
     └── classB_test.py
classA.py:

from datetime import date

class fetchData():
    var = ""

    def __init__(self, thing):
        self.var = thing

    def getInfo(self, x):
        ..process things..
        return info
classA_test.py

import unittest
from classA import fetchData

class classA(unittest.TestCase):
    def testStuff(self):
        ..testStuff..
import unittest
from src.classA import fetchData

class classA(unittest.TestCase):
    def testStuff(self):
        ..testStuff..
settings.json

{
    "python.testing.unittestArgs": [
        "-v",
        "-s",
        "./tests",
        "-p",
        "*_test.py"
    ],
    "python.testing.pytestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.testing.unittestEnabled": true
}
据此,

您可以在测试中执行以下操作:

# import the package
import antigravity

# import the antigravity module
from antigravity import antigravity

# or an object inside the antigravity module
from antigravity.antigravity import my_object
但请确保在每个目录中添加了
\uuuu init\uuuuuuuy.py
文件,使其成为一个包(可以将其保留为空)

希望能奏效。

根据这一点

您可以在测试中执行以下操作:

# import the package
import antigravity

# import the antigravity module
from antigravity import antigravity

# or an object inside the antigravity module
from antigravity.antigravity import my_object
但请确保在每个目录中添加了
\uuuu init\uuuuuuuy.py
文件,使其成为一个包(可以将其保留为空)


希望有效。

正确的格式如下:

repo
├── .vscode
│   ├── settings.json
├── src
│   ├── classA.py
│   └── classB.py
└── tests
     ├── __init__.py     (empty file)
     ├── classA_test.py
     └── classB_test.py
classA_test.py

import unittest
from classA import fetchData

class classA(unittest.TestCase):
    def testStuff(self):
        ..testStuff..
import unittest
from src.classA import fetchData

class classA(unittest.TestCase):
    def testStuff(self):
        ..testStuff..

正确的格式如下:

repo
├── .vscode
│   ├── settings.json
├── src
│   ├── classA.py
│   └── classB.py
└── tests
     ├── __init__.py     (empty file)
     ├── classA_test.py
     └── classB_test.py
classA_test.py

import unittest
from classA import fetchData

class classA(unittest.TestCase):
    def testStuff(self):
        ..testStuff..
import unittest
from src.classA import fetchData

class classA(unittest.TestCase):
    def testStuff(self):
        ..testStuff..

这似乎不起作用。我仍然遇到同样的问题:(我认为从src.classA import*执行
应该会起作用。你试过吗?几个月前这对我有用,但现在不行了。有些事情发生了变化,但我不知道是什么。这似乎不起作用。我仍然遇到同样的问题:(例如,我认为从src.classA import*
执行
应该是可行的。你尝试过吗?几个月前,这对我来说是可行的,但现在不行了。有些事情已经改变了,但我不知道是什么。