gpt4 book ai didi

python - 为什么在 Jupiter Notebook 上执行单元测试时会出现 AttributeError?

转载 作者:行者123 更新时间:2023-12-01 08:05:36 26 4
gpt4 key购买 nike

为了解决 Jupiter Notebook 上的练习,我需要对名为 city_function 的函数执行单元测试

def city_function(city, country):

output = city.title() + ', ' + country.title()

return output

该函数存储在“city_functions.py”中。执行单元测试的代码存储在“test_cities2.ipynb”中。我尝试了以下代码来进行单元测试:

import unittest 



from city_functions import city_function


class CityCountryTestCase(unittest.TestCase):

# Verify if city_function works



def test_city_country_function(self):

output = city_function('lisbon', 'portugal')

self.assertEqual(output, 'Lisbon, Portugal')



unittest.main()

我收到了以下类型的 AttributeError:AttributeError: module 'ma​​in' has no attribute。我该怎么做才能解决这个问题?

最佳答案

有一个不错的article ,描述了您的问题:

The reason is that unittest.main looks at sys.argv and first parameter is what started IPython or Jupyter, therefore the error about kernel connection file not being a valid attribute. Passing explicit list to unittest.main will prevent IPython and Jupyter look at sys.argv. Passing exit=False will prevent unittest.main to shutdown the kernell process

你的最后一行应该是这样的:

unittest.main(argv=['first-arg-is-ignored'], exit=False)

关于python - 为什么在 Jupiter Notebook 上执行单元测试时会出现 AttributeError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55551187/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com