0

嗨,我是 python 新手,正在尝试编写工厂男孩测试,但收到此错误

self.client.login(username=user.first_name, password='abc')
NameError: global name 'user' is not defined

class UserFactory(factory.Factory):
class Meta:
    model = User

first_name = factory.Sequence(lambda n: "Agent %03d" % n)
last_name = 'Steve'
email = factory.LazyAttribute(lambda obj: '%s@example.com' % obj.first_name)


class UserTest(TestCase):

    user = UserFactory()

    def test_user_login_client(self):
        self.client.login(username=user.first_name, password='some')
4

1 回答 1

2

user不是全局名称,而是以下属性UserTest

def test_user_login_client(self):
    self.client.login(username=self.user.first_name, password='some')
于 2016-01-07T20:56:44.090 回答