寫了一下 micropython 的文件系統單元測試


直接上代碼。

主要是測 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 os
FLASH = '/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 support
def 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 rename
def 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 close

def 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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM