Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用VSCode、python和绝对导入时未发现任何测试_Python_Unit Testing_Visual Studio Code_Windows 10_Python Unittest - Fatal编程技术网

使用VSCode、python和绝对导入时未发现任何测试

使用VSCode、python和绝对导入时未发现任何测试,python,unit-testing,visual-studio-code,windows-10,python-unittest,Python,Unit Testing,Visual Studio Code,Windows 10,Python Unittest,我试图将单元测试添加到我的python项目中,但我无法让VS代码发现我的测试。问题是当我尝试导入我正在测试的类时 如果我尝试运行测试文件,它就会通过 如果我从.myfile import myfile中省略,则会发现测试 问题:我做错了什么 文件结构: root ├── A │ ├── __init__.py │ └── myfile.py └── tests ├── __init__.py └── A ├── __init__.py

我试图将单元测试添加到我的python项目中,但我无法让VS代码发现我的测试。问题是当我尝试导入我正在测试的
类时

  • 如果我尝试运行测试文件,它就会通过
  • 如果我从.myfile import myfile中省略
    ,则会发现测试
问题:我做错了什么


文件结构

root
├── A
│   ├── __init__.py
│   └── myfile.py
└── tests
    ├── __init__.py
    └── A
        ├── __init__.py
        └── test_myfile.py
class MyFile(object):
    def __init__(self):
        self.value = 1
import unittest
from A.myfile import MyFile

class Test_MyFile(unittest.TestCase):
    def test_my_file(self):
        self.assertTrue(True)

if __name__ == "__main__":
    unittest.main()
PYTHONPATH=PATH_TO_ROOT
{
    "python.pythonPath": "PATH_TO_PYTHON\python.exe",
    "python.testing.unittestArgs": [
        "-v",
        "-s",
        "./tests",
        "-p",
        "test_*.py",
        "-t",
        "."
    ],
    "python.envFile": "${workspaceFolder}/.env",
    "python.testing.pytestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.testing.unittestEnabled": true
}
start
myfile.py

root
├── A
│   ├── __init__.py
│   └── myfile.py
└── tests
    ├── __init__.py
    └── A
        ├── __init__.py
        └── test_myfile.py
class MyFile(object):
    def __init__(self):
        self.value = 1
import unittest
from A.myfile import MyFile

class Test_MyFile(unittest.TestCase):
    def test_my_file(self):
        self.assertTrue(True)

if __name__ == "__main__":
    unittest.main()
PYTHONPATH=PATH_TO_ROOT
{
    "python.pythonPath": "PATH_TO_PYTHON\python.exe",
    "python.testing.unittestArgs": [
        "-v",
        "-s",
        "./tests",
        "-p",
        "test_*.py",
        "-t",
        "."
    ],
    "python.envFile": "${workspaceFolder}/.env",
    "python.testing.pytestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.testing.unittestEnabled": true
}
start
测试我的文件.py

root
├── A
│   ├── __init__.py
│   └── myfile.py
└── tests
    ├── __init__.py
    └── A
        ├── __init__.py
        └── test_myfile.py
class MyFile(object):
    def __init__(self):
        self.value = 1
import unittest
from A.myfile import MyFile

class Test_MyFile(unittest.TestCase):
    def test_my_file(self):
        self.assertTrue(True)

if __name__ == "__main__":
    unittest.main()
PYTHONPATH=PATH_TO_ROOT
{
    "python.pythonPath": "PATH_TO_PYTHON\python.exe",
    "python.testing.unittestArgs": [
        "-v",
        "-s",
        "./tests",
        "-p",
        "test_*.py",
        "-t",
        "."
    ],
    "python.envFile": "${workspaceFolder}/.env",
    "python.testing.pytestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.testing.unittestEnabled": true
}
start
.env

root
├── A
│   ├── __init__.py
│   └── myfile.py
└── tests
    ├── __init__.py
    └── A
        ├── __init__.py
        └── test_myfile.py
class MyFile(object):
    def __init__(self):
        self.value = 1
import unittest
from A.myfile import MyFile

class Test_MyFile(unittest.TestCase):
    def test_my_file(self):
        self.assertTrue(True)

if __name__ == "__main__":
    unittest.main()
PYTHONPATH=PATH_TO_ROOT
{
    "python.pythonPath": "PATH_TO_PYTHON\python.exe",
    "python.testing.unittestArgs": [
        "-v",
        "-s",
        "./tests",
        "-p",
        "test_*.py",
        "-t",
        "."
    ],
    "python.envFile": "${workspaceFolder}/.env",
    "python.testing.pytestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.testing.unittestEnabled": true
}
start
settings.json

root
├── A
│   ├── __init__.py
│   └── myfile.py
└── tests
    ├── __init__.py
    └── A
        ├── __init__.py
        └── test_myfile.py
class MyFile(object):
    def __init__(self):
        self.value = 1
import unittest
from A.myfile import MyFile

class Test_MyFile(unittest.TestCase):
    def test_my_file(self):
        self.assertTrue(True)

if __name__ == "__main__":
    unittest.main()
PYTHONPATH=PATH_TO_ROOT
{
    "python.pythonPath": "PATH_TO_PYTHON\python.exe",
    "python.testing.unittestArgs": [
        "-v",
        "-s",
        "./tests",
        "-p",
        "test_*.py",
        "-t",
        "."
    ],
    "python.envFile": "${workspaceFolder}/.env",
    "python.testing.pytestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.testing.unittestEnabled": true
}
start
Python测试日志

root
├── A
│   ├── __init__.py
│   └── myfile.py
└── tests
    ├── __init__.py
    └── A
        ├── __init__.py
        └── test_myfile.py
class MyFile(object):
    def __init__(self):
        self.value = 1
import unittest
from A.myfile import MyFile

class Test_MyFile(unittest.TestCase):
    def test_my_file(self):
        self.assertTrue(True)

if __name__ == "__main__":
    unittest.main()
PYTHONPATH=PATH_TO_ROOT
{
    "python.pythonPath": "PATH_TO_PYTHON\python.exe",
    "python.testing.unittestArgs": [
        "-v",
        "-s",
        "./tests",
        "-p",
        "test_*.py",
        "-t",
        "."
    ],
    "python.envFile": "${workspaceFolder}/.env",
    "python.testing.pytestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.testing.unittestEnabled": true
}
start
VSCode版本:1.38.1

Python版本:2.7.14(x64)

这可能是因为为
-t
设置了值。如果
unittest
-
解释为使用默认设置,那么它将使用
-s
,这是
测试
目录,因此,当找不到
A.myfile
时,您的
import A
行导入的是
tests/A/\uuuu init\uuu.py
,而不是
A/\uuu init\uuuuuuu.py
,这会导致
importorror
(检查测试输出以查看失败)


-
更改为
,它将有望修复项目的顶级目录。

这可能是因为为
-t
设置了值。如果
unittest
-
解释为使用默认设置,那么它将使用
-s
,这是
测试
目录,因此,当找不到
A.myfile
时,您的
import A
行导入的是
tests/A/\uuuu init\uuu.py
,而不是
A/\uuu init\uuuuuuu.py
,这会导致
importorror
(检查测试输出以查看失败)


-
更改为
,它将有望修复项目的顶部目录。

是否将根文件夹添加到路径中
cd/path/to/root&&export path=$path:$(pwd)
仅对
PYTHONPATH
-这就是为什么我有
.env
文件。我在Windows 10上。您的根文件夹是否已添加到路径中
cd/path/to/root&&export path=$path:$(pwd)
仅对
PYTHONPATH
-这就是为什么我有
.env
文件。我使用的是Windows 10。你肯定是对的,但遗憾的是,这并不能解决问题。我已经纠正了问题中的错误。晚上我一直在想这个问题,这是我的问题,python没有查看
root/A/
文件夹,因为它已经找到了
root/test/A
文件夹,该文件夹离测试文件更近?@Chau我刚刚注意到您使用的是python 2,而不是python 3。从|未来|导入绝对(import absolute)导入中添加
,看看这是否能解决问题。我已经尝试过了,即使是在python 3环境中,但仍然没有发现测试结果:|@Chau您是否在终端中尝试过这种方法并使其正常工作?基本上,扩展没有做任何神奇的事情,因此您可以从配置中获取这些参数,并在终端中使用它们,直到它适合您为止()。否则,我会将您的
tests/A
目录重命名为
tests/test\u A
,以避免程序包名称冲突,或者尝试使用pytest进行发现(它仍然允许您使用unittest编写测试)。您肯定是对的,但遗憾的是,它没有解决问题。我已经纠正了问题中的错误。晚上我一直在想这个问题,这是我的问题,python没有查看
root/A/
文件夹,因为它已经找到了
root/test/A
文件夹,该文件夹离测试文件更近?@Chau我刚刚注意到您使用的是python 2,而不是python 3。从|未来|导入绝对(import absolute)导入中添加
,看看这是否能解决问题。我已经尝试过了,即使是在python 3环境中,但仍然没有发现测试结果:|@Chau您是否在终端中尝试过这种方法并使其正常工作?基本上,扩展没有做任何神奇的事情,因此您可以从配置中获取这些参数,并在终端中使用它们,直到它适合您为止()。否则,我会将您的
tests/A
目录重命名为
tests/test\u A
,以避免包名冲突,或者尝试使用pytest进行发现(它仍然允许您使用unittest编写测试)。