用鼻子进行单元测试:在编译时测试?
鼻子单元测试框架是否可以在模块的编译阶段执行测试?
事实上,我想测试具有以下结构的内容:
x = 123
# [x is used here...]
def test_x():
assert (x == 123)
del x # Deleted because I don't want to clutter the module with unnecessary attributes
nosetests 告诉我 x 未定义,因为它显然在导入模块后运行 test_x() 。 有没有办法让鼻子在编译阶段执行测试,同时让模块在使用后释放不必要的资源?
Is it possible for the nose unit testing framework to perform tests during the compilation phase of a module?
In fact, I'd like to test something with the following structure:
x = 123
# [x is used here...]
def test_x():
assert (x == 123)
del x # Deleted because I don't want to clutter the module with unnecessary attributes
nosetests tells me that x is undefined, as it apparently runs test_x() after importing the module. Is there a way of having nose perform test during the compilation phase while having the module free unnecessary resources after using them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
处理这个问题的一个简单方法是使用一个 TESTING 标志,并写入:
但是,您不会真正正确地测试您的模块,因为测试将在与您的代码不同的环境下运行。
正确的答案是,您不应该真正费心手动清理变量,除非您确实因此而遇到了一些主要的性能问题。 阅读过早优化,这是一个重要的概念。 解决你所遇到的问题,而不是那些你有一天可能会遇到的问题。
A simple way to handle this would be to have a TESTING flag, and write:
However, you won't really be properly testing your modules as the tests will be running under different circumstances to your code.
The proper answer is that you shouldn't really be bothering with manually cleaning up variables, unless you have actually had some major performance problems because of them. Read up on Premature Optimization, it's an important concept. Fix the problems you have, not the ones you maybe could have one day.
根据nose的主要开发人员Jason Pellerin的说法,nose单元测试框架 无法在编译期间运行测试。 如果模块“构造”和测试例程都需要访问某个变量(在没有测试的情况下将被删除),这将是一个潜在的烦恼。
一种选择是通过在名称前添加“__”来阻止用户使用任何这些不必要的保存变量(这也适用于类构造中使用的变量:它们可以是这些“私有”全局变量之一)。
另一个也许更干净的选择是专用于该任务的模块:该模块将包含由模块“本身”(即没有测试)及其测试共享的变量(如果不是为了测试)。
这些选项的问题在于,如果没有测试则可以删除的变量反而保留在内存中,只是因为测试代码使用它们更好。 至少,有了上述两个选项,用户不应该被诱惑去使用这些变量,也不应该觉得有必要想知道它们是什么!
According to nose's main developer Jason Pellerin, the nose unit testing framework cannot run tests during compilation. This is a potential annoyance if both the module "construction" and the test routines need to access a certain variable (which would be deleted in the absence of tests).
One option is to discourage the user from using any of these unnecessarily saved variables by prepending "__" to their name (this works also for variables used in class construction: they can be one of these "private" globals).
Another, perhaps cleaner option is to dedicate a module to the task: this module would contain variables that are shared by the module "itself" (i.e. without tests) and its tests (and that would not have to be shared were it not for the tests).
The problem with these option is that variables that could be deleted if there were no tests are instead kept in memory, just because it is better for the test code to use them. At least, with the above two options, the user should not be tempted to use these variables, nor should he feel the need to wonder what they are!