简体   繁体   English

Python unittest /测试装置测试是否已加载模块

[英]Python unittest/test fixture testing if module is loaded

I'm using another SO recommendation for importing simplejson , which reads as follows: 我正在使用另一个SO推荐来导入simplejson ,内容如下:

try:
    import simplejson as json
except ImportError:
    import json

However, in writing my unittest to see if "one of the json modules" has in fact been imported, I'm stuck in the circular nightmare that is my own design! 但是,在编写单元测试以查看是否实际上已导入“ json模块之一”时,我陷入了自己设计的循环梦night! /sigh. /叹。

class AreAllModulesLoaded(unit.TestCase):
    """Test to make sure all modules are loaded"""
    def test_json(self):
        try:
            self.assertTrue("simplejson" in sys.modules)
        except:
            try:
                self.assertTrue("json" in sys.modules)
            except AssertionError:
                raise

I thought something like this would have worked, however, on inspection of the unittest.failUnless or unittest.assertTrue (I have tried both), it raises the failure and the test stops (it's failing because I am causing the failure by only loading json instead of simplejson )... 我以为这样的事情本来可以工作,但是,在检查unittest.failUnlessunittest.assertTrue (我都尝试过)时,它会引发失败并停止测试(失败了,因为我仅通过加载json导致失败而不是simplejson )...

My intended goal is I want my unittest to confirm that EITHER json or simplejson was loaded. 我的预期目标是我希望我的单元测试确认已加载EITHER json或simplejson。 How might I go about this? 我该怎么办?

I have thought about capturing "truthiness" prior to assertion, and then only pass "True" or "False", but that doesn't feel right for unittesting only because I thought that this should have been part the unittest proper rather than a work-around (that's my opinion, maybe you feel differently). 我曾考虑过在断言之前捕获“真实性”,然后仅通过“ True”或“ False”,但这不适用于单元测试,因为我认为这应该是单元测试的一部分,而不是一项工作。周围(这是我的观点,也许您有不同的看法)。 For example, I had thought about doing something like (python-esque pseduoish code): 例如,我曾考虑过要做类似的事情(python-esque pseduoish代码):

_simplejsonLoaded = "simplejson" in sys.modules
_jsonLoaded = "json" in sys.modules

self.assertTrue(_simplejsonLoaded or _jsonLoaded)

(I am new to formal unittesting - so, if something seems crazy here please let me know). (我是正式进行单元测试的新手-因此,如果这里有些疯狂的地方,请告诉我)。

Other information: 其他信息:

  • Im using Python 2.6 (for work reasons - can't change that) 我正在使用Python 2.6(出于工作原因-无法更改)
  • Windows and Linux Windows和Linux

I'm 我是

I don't think you need the try/except stuff in the TestCase 我认为您不需要TestCase中的try / except东西

import sys
import unittest
try:
    import simplejson as json
except ImportError:
    import json

class TestSomething(unittest.TestCase):
    def test_json(self):
        if ('json' in sys.modules or 'simplejson' in sys.modules):
            self.assert_(True, "some kind of json loaded")
        else:
            self.fail()

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

Well, the question is whether you really care which particular library you have imported. 好吧,问题是您是否真的关心导入的是哪个特定库。 When you import simplejson as json then you json symbol in sys.modules as well, and that's what really matters, right? 当您将simplejson as json导入时,您也将sys.modules json符号也添加进来,那sys.modules真正重要的,对吗? Of course, you can then check whether the imported json library has all methods and properties required for your own program, but that's another issue. 当然,您可以然后检查导入的json库是否具有您自己的程序所需的所有方法和属性,但这是另一个问题。 So, isn't the correct definition of the test just: 因此,测试的正确定义不仅是:

import sys
import my_own_module

class TestSomething(unittest.TestCase):
    def test_json(self):
        self.assertIn('json', sys.modules,
                      "json API not loaded")

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM