为什么在Python测试中,逻辑上应该是n

2024-09-28 15:37:11 发布

您现在位置:Python中文网/ 问答频道 /正文

class TestUM:

    @classmethod
    def setup_class(will):
        """ Setup Class"""

        will.var = "TEST"

    def setup(this):
        """ Setup """

        print this.var

    def test_number(work):
        """ Method """

        print work.var


    def teardown(orr):
        """ Teardown """

        print orr.var

    @classmethod
    def teardown_class(nott):
        """ Teardown Class """

        print nott.var

按以下方式运行

nosetests -v -s test.py

我不是Python专家,但我不明白为什么上面的代码可以完美地使用nose。每一张打印“测试”。这里到底发生了什么。你知道吗


Tags: testvardefsetupthiswillclasswork
1条回答
网友
1楼 · 发布于 2024-09-28 15:37:11

在实例方法中,第一个参数是实例本身。你知道吗

在类方法中,第一个参数是类本身。你知道吗

在您的例子中,不是将该参数命名为selfcls(约定),而是将其命名为thisworkorrnott。但是他们都得到了相同的参数,不管参数的名称是什么。你知道吗

您已经成功地将属性var设置为"TEST",因此他们都能正确地看到它。你知道吗


不使用类的示例函数:

def test1(attribute):
    print attribute

def test2(name):
    print name

def test3(cls):
    print cls

def test4(self):
    print self

调用这些函数:

>>> test1('hello')
hello
>>> test2('hello')
hello
>>> test3('hello')
hello
>>> test4('hello')
hello

参数的名称无关紧要。重要的是参数指向什么,它总是实例或类

相关问题 更多 >