【发布时间】:2022-01-11 20:21:48
【问题描述】:
我的函数中定义了一个列表 CountryList[],我想检查它 不是空的。我将其初始化为空,但稍后在函数中将数据放入其中。 这是我输入的单元测试。
def assertEmpty(self, CountryList):
self.assertFalse(CountryList)
def assertNotEmpty(self, CountryList):
self.assertTrue(CountryList)
这是我程序中的方法。
def onCountry(self, doc_id):
if(doc_id==None):
return
output_list = self.findBySubjectDocId(doc_id)
country_list=[]
for x in output_list:
country_id=x["visitor_country"]
if(SHOW_FULL_NAMES):
country_id=pc.country_alpha2_to_country_name(country_id)
country_list.append(country_id)
ts=pd.Series(country_list).value_counts().plot(kind='bar',color='purple')
plt.xticks(rotation='horizontal')
plt.xlabel('Country')
plt.ylabel('Number of Viewers')
plt.title("Viewers based on Country")
ts.plot()
plt.show()
print("Countries of Visitors:")
x = []
y = []
for k,v in Counter(country_list).items():
x.append(k)
y.append(v)
print(k,"-",v)
您是否建议我以其他方式测试此代码?或者上述测试是否可以接受?
【问题讨论】:
-
你在哪里创建这个
CountryList?它是在测试中以某种方式创建的还是在您的代码中创建的?你的assertEmpty和assertNotEmpty函数真的是全局的还是属于一个类? -
@Code-Apprentice CountryList 位于名为 program 的 .py 文件中,assertEmpty 和 asertNotEmpty 位于另一个 Test_Browser.py 文件中
-
请edit你的代码解释文件结构。有关创建示例代码 sn-p 的更多提示,请阅读minimal reproducible example。
标签: python unit-testing testing pycharm histogram