带Python的Visual Studio:参考解决方案中的另一个项目

时间:2020-07-20 11:13:10

标签: python unit-testing visual-studio-2013

对于Visual Studio 2013,我试图通过两个项目设置python解决方案。一个项目保存源文件,另一项目保存单元测试。这是基本的文件夹结构:

  • PythonApplication1
    • MyOwnClass.py
  • PythonApplication1.UnitTests
    • Test_MyOwnClass.py

现在我有问题,在单元测试项目中无法识别源文件:

Test Name:  test_A
Test Outcome:   Failed
Result StandardError:   
Traceback (most recent call last):
  File "[...]\visualstudio_py_testlauncher.py", line 69, in <module>
    main()
  File "[...]\visualstudio_py_testlauncher.py", line 60, in main
    __import__(opts.module)
  File "[PathToSolution]\PythonApplication1\PythonApplication1.UnitTests\Test_MyOwnClass.py", line 2, in <module>
    from MyOwnClass import MyOwnClass
ModuleNotFoundError: No module named 'MyOwnClass'

这是源文件PythonApplication1 / MyOwnClass.py中的代码:

class MyOwnClass:
   attribute1 = 0
   attribute2 = []

这是单元测试文件PythonApplication1.UnitTests / Test_MyOwnClass.py中的代码:

import unittest
from MyOwnClass import MyOwnClass

class Test_test1(unittest.TestCase):
    def test_A(self):
        myOwn = MyOwnClass()
        self.assertEqual( 0, myOwn.attribute1 )

我已经尝试通过使用具有以下内容的PythonSettings.json文件来集成源代码项目:

{
   "TestFramework": "unittest",
   "UnitTestRootDirectory": "PythonApplication1.UnitTests",
   "UnitTestPattern": "Test_*.py",
   "SearchPaths": [ "..\\PythonApplication1" ]
}

我也尝试输入非相对路径,但最终得到了相同的结果。

我知道可以使用sys.path功能添加路径。但是我想将其发布给整个单元测试项目,而不仅仅是一个文件。

0 个答案:

没有答案
相关问题