博客
关于我
写了一下 micropython 的文件系统单元测试
阅读量:432 次
发布时间:2019-03-06

本文共 2910 字,大约阅读时间需要 9 分钟。

直接上代码。

主要是测 ram 、rom 、还有 剩余空间、目录管理、文件管理、分区边界测试,还是要结合自身的需求补充更多测试的。

比如没有做完整的读写正确性测试,擦除次数测试等等。

### 获取芯片的 ram 大小#def print_mem_free():    #import gc    #print('ram total : ' + str(gc.mem_free() / 1024) + ' kb')#print_mem_free()#gc.collect()#print_mem_free()# 文件系统测试import osFLASH = '/flash'## 获取 spiffs 映射的 flash 分区大小def print_flash_size(FLASH):    statvfs_fields = ['bsize', 'frsize', 'blocks', 'bfree', 'bavail', 'files', 'ffree', ]    info = dict(zip(statvfs_fields, os.statvfs(FLASH)))    # print(info)    print('flash total : ' + str(info['bsize'] * info['bfree'] / 1024) + ' kb')## 格式化 flash 文件系统def unit_test_fs_format():    os.flash_format()#unit_test_fs_format()#print_flash_size(FLASH)## 测试目录相关函数 不支持 # NotImplementedError: SPIFFS not supportdef unit_test_fs_dir_mk_and_rm(FLASH):    assert(0 == len(os.listdir(FLASH)))    os.mkdir('test')    os.rmdir('test')    assert(0 == len(os.listdir(FLASH)))#unit_test_fs_dir_mk_and_rm(FLASH)#print_flash_size(FLASH)## 测试文件相关函数 open stat remove renamedef unit_test_fs_file_function(FLASH):    name, info = 't.txt', b'0123456789ABCDEF'    # 创建文件    few = open(name, "wb")    few.write(info)    #assert(os.stat(name)[6] == 0) # 可以在 menuconfig 中取消 cache 机制    print(os.stat(name))    # 文件应该存在了,但内容还未写入,此时则证明有 write cache 工作。    assert(name in os.listdir(FLASH))    few.close()    # 检查文件是否存在,且文件大小为 len(info) 。    assert(os.stat(name)[6] == len(info))    # 确认文件读取    fer = open(name, "rb")    assert(fer.read() == info)    fer.close()    # 确认 rename 工作    tmp = 'rename.txt'    os.rename(name, tmp)    assert(tmp in os.listdir(FLASH))    os.rename(tmp, name)    assert(name in os.listdir(FLASH))    os.remove(name)    assert(name not in os.listdir(FLASH))unit_test_fs_file_function(FLASH)print_flash_size(FLASH)## 测试文件的边界与重入 file write read closedef unit_test_fs_file(FLASH):    ### 追加写入测试。    name, info = 't.txt', b'0123456789ABCDEF'    if (name in os.listdir(FLASH)):        os.remove(name)    few = open(name, "wb")    few.write(info)    few.close()    ### 测试内容    few = open(name, "ab")    assert(few.read() == info)    few.write(name)    few.close()    few = open(name, "ab")    assert(few.read() == info + name)    few.close()    if (name in os.listdir(FLASH)):        os.remove(name)    ## 边界检查    import time, gc    count, tm = 0, time.ticks_ms()    info = info * 10240    print(len(info), time.ticks_diff(time.ticks_ms(), tm))    gc.collect()    try:        few = open(name, "wb")        while True:            print(few.write(info)) # 使用的是无 spiffs cache 的固件写入速度较慢。            #print(few.flush())            count = count + 1            print(count * len(info))            print_flash_size(FLASH)        few.close()    except Exception as e:        print(e)    finally:        print(count * len(info), time.ticks_diff(time.ticks_ms(), tm))        print_flash_size(FLASH)        few.close()    ## 数据检查    ## 写入重入unit_test_fs_file(FLASH)print_flash_size(FLASH)

转载地址:http://inbyz.baihongyu.com/

你可能感兴趣的文章
Navicat向sqlserver中插入数据时提示:当 IDENTITY_INSERT 设置为 OFF 时,不能向表中的标识列插入显式值
查看>>
Navicat因导入的sql文件中时间数据类型有参数而报错的原因(例:datetime(3))
查看>>
Navicat如何连接MySQL
查看>>
navicat导入.sql文件出错2006- MySQLserver has gone away
查看>>
Navicat导入海量Excel数据到数据库(简易介绍)
查看>>
Navicat工具Oracle数据库复制 or 备用、恢复功能(评论都在谈论需要教)
查看>>
Navicat工具中建立数据库索引
查看>>
navicat工具查看MySQL数据库_表占用容量_占用空间是多少MB---Linux工作笔记048
查看>>
navicat怎么导出和导入数据表
查看>>
Navicat怎样同步两个数据库中的表
查看>>
Navicat怎样筛选数据
查看>>
Navicat报错connection is being used
查看>>
Navicat报错:1045-Access denied for user root@localhost(using passwordYES)
查看>>
Navicat控制mysql用户权限
查看>>
navicat操作mysql中某一张表后, 读表时一直显示正在载入,卡死不动,无法操作
查看>>
Navicat连接mysql 2003 - Can't connect to MySQL server on ' '(10038)
查看>>
Navicat连接mysql数据库中出现的所有问题解决方案(全)
查看>>
Navicat连接Oracle出现Oracle library is not loaded的解决方法
查看>>
Navicat连接Oracle数据库以及Oracle library is not loaded的解决方法
查看>>
Navicat连接sqlserver提示:未发现数据源名并且未指定默认驱动程序
查看>>