在运行unittest时获得“未找到测试”

时间:2018-05-17 09:28:58

标签: python pycharm

如果我在PyCharm中单击“运行'Unittest for ..'”,则会打印:

Launching unittests with arguments python -m unittest /home/sfalk/workspace/git/m-search/python/tests/cluster/pipeline/preprocessing.py in /home/sfalk/workspace/git/m-search/python/tests/cluster/pipeline

和报告

  

未找到任何测试。

但是,如果我复制PyCharm声称要运行的完全行,即

python -m unittest /home/sfalk/workspace/git/m-search/python/tests/cluster/pipeline/preprocessing.py

然后我得到了我预期的输出。

$ python -m unittest /home/sfalk/workspace/git/m-search/python/tests/cluster/pipeline/preprocessing.py
..
+---+------------+
| _1|          _2|
+---+------------+
|  1|Hello World!|
+---+------------+

.
----------------------------------------------------------------------
Ran 1 test in 4.998s

OK

运行配置说:

  

脚本路径: /home/sfalk/workspace/git/m-search/python/tests/cluster/pipeline/preprocessing.py

     

工作目录: /home/sfalk/workspace/git/m-search/python/tests/cluster/pipeline

我没有得到这个......

imho应该没关系,但这是单元测试的代码:

import unittest

from pyspark import SparkContext, SparkConf, SQLContext


class TestPreprocessingText(unittest.TestCase):

    def test_preprocessingText(self):

        conf = SparkConf()
        conf.setMaster('local')
        conf.setAppName('MyApp')
        sc = SparkContext.getOrCreate()

        sql = SQLContext(sc)

        df = sql.createDataFrame(
            [
                (1, 'Hello World!')
            ]
        )

        df.show()


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

1 个答案:

答案 0 :(得分:2)

尝试按照惯例重命名测试文件名:

app_folder/
    appname.py
    test_appname.py

如上所示,您尝试测试的应用程序和测试文件位于该命名约定之后的同一目录中。