简体   繁体   English

unittest:AttributeError:模块'__main__'没有属性'C:\ ...'

[英]unittest: AttributeError: module '__main__' has no attribute 'C:\…'

I am trying to run test in Python.我正在尝试在 Python 中运行测试。 Simplified it looks like this:简化后看起来像这样:

#import librarys
import unittest

def methodToTest (df1, df2):
     #do something
     df3 = createDf3()
     return df3

#define test data
df4 = someDf
df5 = anotherDf

class TestMethodToTest(unittest.TestCase):
    def test_m (self):
        result = methodToTest(someDf, anotherDf)
        self.assertEqual(len(result), 2)

if __name__ == '__main__':
    unittest.main()

so I have a method "methodToTest" and and I create some inputs "df4" and "df5".所以我有一个方法“methodToTest”并且我创建了一些输入“df4”和“df5”。 I know that if you put the inputs in the method, the result should be a dataframe with 2 rows.我知道如果你把输入放在方法中,结果应该是一个有 2 行的 dataframe。 to ensure, that it is running correctly i wanted to write a unittest.为了确保它正确运行,我想编写一个单元测试。 But if i try to start the test, the following error massage is displayed:但是,如果我尝试开始测试,则会显示以下错误消息:

E
======================================================================
ERROR: C:\...\runtime\kernel-... (unittest.loader._FailedTest)
----------------------------------------------------------------------
AttributeError: module '__main__' has no attribute 'C:\...\jupyter\runtime\kernel-...'

----------------------------------------------------------------------
Ran 1 test in 0.002s

FAILED (errors=1)
An exception has occurred, use %tb to see the full traceback.

SystemExit: True
C:\...\interactiveshell.py:...: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

I do not know, why this error occurs and how to avoid it.我不知道为什么会发生此错误以及如何避免它。

The reason is that unittest.main looks at sys.argv and first parameter is what started IPython or Jupyter,原因是 unittest.main 查看 sys.argv 而第一个参数是启动 IPython 或 Jupyter,

therefore the error about kernel connection file not being a valid attribute.因此有关 kernel 连接文件的错误不是有效属性。

Passing explicit list to unittest.main will prevent IPython and Jupyter look at sys.argv.将显式列表传递给 unittest.main 将阻止 IPython 和 Jupyter 查看 sys.argv。

Passing exit=False will prevent unittest.main to shutdown the kernell process传递 exit=False 将阻止 unittest.main 关闭 kernell 进程

https://medium.com/@vladbezden/using-python-unittest-in-ipython-or-jupyter-732448724e31 https://medium.com/@vladbezden/using-python-unittest-in-ipython-or-jupyter-732448724e31

if __name__ == '__main__':
    unittest.main(argv=['first-arg-is-ignored'], exit=False)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:模块'__main__'没有属性'cleaner' - AttributeError: module '__main__' has no attribute 'cleaner' AttributeError: 模块 '__main__' 没有属性 'AverageWordLengthExtractor' - AttributeError: module '__main__' has no attribute 'AverageWordLengthExtractor' pickle/joblib AttributeError: 模块 &#39;__main__&#39; 在 pytest 中没有属性 &#39;thing&#39; - pickle/joblib AttributeError: module '__main__' has no attribute 'thing' in pytest TensorFlow:模块&#39;__main__&#39;没有属性&#39;main&#39; - TensorFlow: module '__main__' has no attribute 'main' 查找“pip”的模块规范时出错(AttributeError:模块“__main__”没有属性“__file__”) - Error while finding module specification for 'pip' (AttributeError: module '__main__' has no attribute '__file__') Pydoop mapreduce“ AttributeError:模块&#39;wordcount_minimal&#39;没有属性&#39;__main__&#39;” - Pydoop mapreduce “AttributeError: module 'wordcount_minimal' has no attribute '__main__'” 用于单元测试的python3:AttributeError:模块&#39;__main__&#39;没有属性“内核......” - python3 for unit test: AttributeError: module '__main__' has no attribute “kernel…” Python多处理错误:AttributeError:模块&#39;__main__&#39;没有属性&#39;__spec__&#39; - Python Multiprocessing error: AttributeError: module '__main__' has no attribute '__spec__' AttributeError:模块“unittest”没有属性“Testcase” - AttributeError: module 'unittest' has no attribute 'Testcase' AttributeError的: - AttributeError: <module '__main__' from [..] does not have the attribute 'open'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM