简体   繁体   English

Python 测试断言

[英]Python Test Assert

Writing a test cases to QA a excel file将测试用例写入 QA excel 文件


I am creating this test case to make sure there is no " NA, nan, None, N/A, empty" 我正在创建这个测试用例以确保没有“NA、nan、None、N/A、empty”

Here is the input File image: Dataset Image这是输入文件图像:数据集图像
when I run the code below Column A is passing the test, even-though there is an empty row in line 3.当我运行 A 列下面的代码时,即使第 3 行有一个空行,也通过了测试。

 import unittest import pandas as pd class TestC(unittest.TestCase): def testRowWithNan(self): df= pd.read_excel('abc\test.xlsx','sheet1') tab = df['Emp_Name'] self.assertIsNotNone(tab) if__name__ == '__main__': unittest.main()

Please let me know if any one solution. 如果有任何一种解决方案,请告诉我。

I am not that familiar with pandas but df['Emp_Name'] should return list of all the names, you need to check every name in that column.我对 pandas 不太熟悉,但df['Emp_Name']应该返回所有名称的列表,您需要检查该列中的每个名称。 So you need to create a loop like so:所以你需要像这样创建一个循环:

not_want = ["NA", "nan", None, "N/A", "empty"]
for name in df["Emp_name"]:
    # Checking if any of the words that we do not want is in the name
    # If we have the word that we do not want in the name any() will turn true and
    # the test will fail.
    assert any(word in name for word in not_want) is False 

I haven't used unittest library so can't exactly say how you would test via unittest, but I'd simply test like this:我没有使用 unittest 库,所以不能确切地说你将如何通过 unittest 进行测试,但我只是像这样测试:

names = df.Emp_Name.values
assert not np.isnan(names).any()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM