首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

来看文件处理Python怎么写?

文件处理

处理csv文件

如下为csv文件内容

用户名,密码,重复密码,邮箱,错误提示信息testuser1,123456,654321,hello@123.com,两次密码输入不一致。

使用python自带csv库进行处理

importcsv

importos

datafile=os.path.join('/Users/zack/Desktop/2018-08-25/zuoye','data.csv')

# print(datafile)

defread_csv():

csvdata=[]

withopen(datafile,encoding="utf-8")asf:

print("f=",f)

csvreadrer=csv.reader(f)

print("csvreader==",csvreadrer)

next(csvreadrer)

forrowincsvreadrer:

csvdata.append(row)

returncsvdata

通过csv文件中定义的内容进行解析处理

fromseleniumimportwebdriver

fromselenium.webdriver.common.byimportBy

fromparse_csvimportread_csv

rf=read_csv()

defregister(username,passwd,repasswd,email,tip_message):

driver=webdriver.Chrome()

driver.maximize_window()

driver.implicitly_wait(30)

driver.get("http://118.31.19.120:3000/signup")

driver.find_element(By.ID,"loginname").send_keys(username)

driver.find_element(By.ID,"pass").send_keys(passwd)

driver.find_element(By.ID,'re_pass').send_keys(repasswd)

driver.find_element(By.ID,'email').send_keys(email)

driver.find_element(By.CSS_SELECTOR,".span-primary").click()

# get Tip message Textresult_text=driver.find_element(By.TAG_NAME,'strong').textprint(result_text)

defrun():

forrowinrf:

register(row[],row[1],row[2],row[3],row[4])

defmain():

run()

if__name__=='__main__':

main()

日志处理

python自带日志记录模块,参考如下链接

https://docs.python.org/3/howto/logging-cookbook.html

https://docs.python.org/3/library/logging.html

定位文件get_logging.py

importlogging

deff_w():

# create logger with 'spam_application'logger=logging.getLogger('CNode_club')

logger.setLevel(logging.DEBUG)

# create file handler which logs even debug messagesfh=logging.FileHandler('spam.log')

fh.setLevel(logging.DEBUG)

# create console handler with a higher log level# ch = logging.StreamHandler()# ch.setLevel(logging.ERROR)# create formatter and add it to the handlersformatter=logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

fh.setFormatter(formatter)

# ch.setFormatter(formatter)# add the handlers to the loggerlogger.addHandler(fh)

# logger.addHandler(ch)returnlogger

将日志功能记录到登陆操作中

register.py

fromseleniumimportwebdriver

fromselenium.webdriver.common.byimportBy

fromparse_csvimportread_csv

log=f_w()

defregister(username,passwd,repasswd,email,tip_message):

driver=webdriver.Chrome()

driver.maximize_window()

driver.implicitly_wait(30)

driver.get("http://118.31.19.120:3000/signup")

driver.find_element(By.ID,"loginname").send_keys(username)

log.info('输入用户名:'+username)

# write_logging({'level':'DEBUG', 'action': '输入文本', 'name1': username})driver.find_element(By.ID,"pass").send_keys(passwd)

driver.find_element(By.ID,'re_pass').send_keys(repasswd)

driver.find_element(By.ID,'email').send_keys(email)

driver.find_element(By.CSS_SELECTOR,".span-primary").click()

# get Tip message Textresult_text=driver.find_element(By.TAG_NAME,'strong').textprint(result_text)

defrun():

forrowinrf:

register(row[],row[1],row[2],row[3],row[4])

defmain():

run()

if__name__=='__main__':

main()

test_register.py

importunittest

fromregisterimportregister

fromget_loggingimportf_w

fromparse_csvimportread_csv

log=f_w()

classTestRegister(unittest.TestCase):

deftest_register(self):

# rows = read_csv()# for row in rows:text=register('testuser1','aaaa','1212',"sss@12.com")

self.assertEqual(text,"两次密码输入不一致。")

log.info('验证成功:'+text)

if__name__=='__main__':

unittest.main()

定义TestCase

importunittest

classTestStringMethods(unittest.TestCase):

# testcase的执行顺序 0-9 A-Z a-z@classmethoddefsetUpClass(cls):

print("befor all case ....")

defsetUp(self):

print("before every test case exec ......")

deftearDown(self):

print('after every test case exec ......')

deftesta_upper(self):

print("testaaaa")

self.assertEqual('foo'.upper(),'FOO',"不相等")

deftestA_isupper(self):

self.assertTrue('FO8O'.isupper())

self.assertFalse('Foo'.isupper())

deftest1_split(self):

s='hello world'self.assertEqual(s.split(), ['hello','world'])

# check that s.split fails when the separator is not a stringwithself.assertRaises(TypeError):

s.split(2)

@classmethoddeftearDownClass(cls):

print("after all testcase....")

if__name__=='__main__':

unittest.main()

all_test.py

importunittest

fromtest_case01importTestStringMethods01

fromtest_case02importTestStringMethods02

fromtest_case03importTestStringMethods03

test_cases=(TestStringMethods01,TestStringMethods02,TestStringMethods03)

defload_tests(loader,tests):

suite=unittest.TestSuite()

fortest_classintest_cases:

tests=loader.loadTestsFromTestCase(test_class)

suite.addTests(tests)

returnsuite

loader=unittest.TestLoader()

suite=load_tests(loader,test_cases)

if__name__=='__main__':

runner=unittest.TextTestRunner()

runner.run(suite)

# unittest.runner

load test

importunittest

fromtest_case01importTestStringMethods01

fromtest_case02importTestStringMethods02

fromtest_case03importTestStringMethods03

test_cases=(TestStringMethods01,TestStringMethods02,TestStringMethods03)

defgetsuit():

suite=unittest.TestSuite()

suite.addTest(TestStringMethods01('test1_01split'))

suite.addTest(TestStringMethods02('testA_02isupper'))

returnsuite

defload_tests(loader,tests):

suite=unittest.TestSuite()

fortest_classintest_cases:

tests=loader.loadTestsFromTestCase(test_class)

suite.addTests(tests)

returnsuite

loader=unittest.TestLoader()

suite=load_tests(loader,test_cases)

if__name__=='__main__':

runner=unittest.TextTestRunner()

s=getsuit()

runner.run(s)

# unittest.runner

discover

importunittest

fromunittestimportTestLoader,TestSuite

loader=TestLoader()

tests=loader.discover('./testcases',pattern='test01.py')

t=TestSuite()

t.addTests(tests)

runner=unittest.TextTestRunner()

runner.run(t)

了解更多哦哟,抓出轨,测试更有一山高??拼多多出现超级大bug,损失200亿,这个锅谁来背?基于Python语言的自动化测试

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20190207B04OG500?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券