我如何在python 2.6中测试抽象方法(how can i test an abstract method in python 2.6)

编程入门 行业动态 更新时间:2024-10-26 07:33:47
我如何在python 2.6中测试抽象方法(how can i test an abstract method in python 2.6)

我有一个抽象类:

import abc class Hello(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod def add(self, foo): pass @abc.abstractmethod def remove(self, foo): pass

我正在使用abc做抽象方法,所以,当我这样做时:

hello = Hello()

并引发此错误: TypeError: Can't instantiate abstract class Hello with abstract methods add, remove

所以我可以测试这种类型错误:

self.assertRaises(Exception, Hello) # but this only test the constructor and i can't get the 100% of code coverage. I need call the add method and the remove method

额外的问题:任何人都知道如何在python 2.6中断言消息异常? (你不能使用with: for raise assertions。)

如何测试这种抽象方法以获得100%的代码覆盖率?

I have an abstract class:

import abc class Hello(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod def add(self, foo): pass @abc.abstractmethod def remove(self, foo): pass

I'm using abc for do abstract methods, so, when i do:

hello = Hello()

and this error is raised: TypeError: Can't instantiate abstract class Hello with abstract methods add, remove

So I can test this type error with:

self.assertRaises(Exception, Hello) # but this only test the constructor and i can't get the 100% of code coverage. I need call the add method and the remove method

Extra question: anybody knows how can i assert the message exception in python 2.6? (you can't use the with: for raise assertions.)

How can i test this abstract methods for get the 100% of code coverage?

最满意答案

如何创建抽象方法的文档字符串而不是使用这里提到的pass , https://stackoverflow.com/a/19275908/469992 ? 它还可以用于提供有关该方法在子类中应该执行的操作的一些信息。

abstract.py,

from abc import ABCMeta, abstractmethod class A(object): __metaclass__ = ABCMeta @abstractmethod def some_method(self): "This method should ..." class B(A): def some_method(self): return 1

test_abstract.py,

import unittest import abstract class TestB(unittest.TestCase): def test(self): self.assertEqual(abstract.B().some_method(), 1)

然后,使用python 2.6.8, nosetests --with-xcoverage输出,

. Name Stmts Miss Cover Missing ---------------------------------------- abstract 7 0 100% ---------------------------------------------------------------------- Ran 1 test in 0.004s

What about creating a doc string of the abstract method instead of using pass as mentioned here, https://stackoverflow.com/a/19275908/469992 ? It can also be used to give some information about what the method is supposed to do in the sub classes.

abstract.py,

from abc import ABCMeta, abstractmethod class A(object): __metaclass__ = ABCMeta @abstractmethod def some_method(self): "This method should ..." class B(A): def some_method(self): return 1

test_abstract.py,

import unittest import abstract class TestB(unittest.TestCase): def test(self): self.assertEqual(abstract.B().some_method(), 1)

Then, using python 2.6.8, nosetests --with-xcoverage outputs,

. Name Stmts Miss Cover Missing ---------------------------------------- abstract 7 0 100% ---------------------------------------------------------------------- Ran 1 test in 0.004s

更多推荐

本文发布于:2023-07-26 05:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1271351.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:抽象   测试   方法   如何在   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!