Tensorflow--tf.test.TestCase.get_temp_dir()

本文介绍TensorFlow中tf.test.TestCase基类的get_temp_dir()方法,该方法用于生成唯一临时文件路径,确保测试间无目录冲突。通过示例展示同一测试中返回相同路径,不同测试中返回不同路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Tensorflow–tf.test.TestCase.get_temp_dir()

描述

get_temp_dir()是tf.test的基类TestCase的一个方法,用来在测试的时候,返回一个唯一的临时文件路径。在同一个测试中,此方法返回的路径是唯一的,在不同的测试中,此方法返回的路径不一致。测试结束,临时文件夹自动删除。

示例

import tensorflow as tf

class GetTempDir(tf.test.TestCase):
    def testDir(self):
        with self.test_session():
            print(self.get_temp_dir())
            print(self.get_temp_dir())

if __name__ == "__main__":
    tf.test.main()

运行后,结果如下,不难看出返回了相同的临时文件路径

/var/folders/l9/mx76gx5d5tj3shm2dghyd1jh0000gn/T/unit_testn1yjgy64/tmpezzrtpka
..
/var/folders/l9/mx76gx5d5tj3shm2dghyd1jh0000gn/T/unit_testn1yjgy64/tmpezzrtpka
----------------------------------------------------------------------
Ran 2 tests in 0.150s

OK

将代码修改如下:

import tensorflow as tf

class GetTempDir(tf.test.TestCase):
    def testDir(self):
        with self.test_session():
            print(self.get_temp_dir())
    def testDir1(self):
        with self.test_session():
            print(self.get_temp_dir())

if __name__ == "__main__":
    tf.test.main()

运行后,会产生两个不同的临时文件路径

----------------------------------------------------------------------
Ran 3 tests in 0.169s

OK
/var/folders/l9/mx76gx5d5tj3shm2dghyd1jh0000gn/T/unit_testjs690vbf/tmpfh_lbknn
/var/folders/l9/mx76gx5d5tj3shm2dghyd1jh0000gn/T/unit_testjs690vbf/tmp85uhmynd

这种机制可以避免不同测试产生的临时文件目录冲突。

如果希望在同一个测试中产生不同的临时文件路径,可以使用Python中的tempfile模块的mkdtemp方法。

官网链接

https://tensorflow.google.cn/api_docs/python/tf/test/TestCase#get_temp_dir

FAILED: .llt/src_llt/sd8911/hidsp0/testcase/alg/CMakeFiles/src_llt-sd8911-hidsp0-testcase-alg.dir/odsp_test_tx_fde.cpp.obj "D:\Program Files (x86)\mingw64\bin\g++.exe" @.llt\src_llt\sd8911\hidsp0\testcase\alg\CMakeFiles\src_llt-sd8911-hidsp0-testcase-alg.dir\odsp_test_tx_fde.cpp.obj.rsp -MD -MT .llt/src_llt/sd8911/hidsp0/testcase/alg/CMakeFiles/src_llt-sd8911-hidsp0-testcase-alg.dir/odsp_test_tx_fde.cpp.obj -MF .llt\src_llt\sd8911\hidsp0\testcase\alg\CMakeFiles\src_llt-sd8911-hidsp0-testcase-alg.dir\odsp_test_tx_fde.cpp.obj.d -o .llt/src_llt/sd8911/hidsp0/testcase/alg/CMakeFiles/src_llt-sd8911-hidsp0-testcase-alg.dir/odsp_test_tx_fde.cpp.obj -c ../../../src_llt/sd8911/hidsp0/testcase/alg/odsp_test_tx_fde.cpp In file included from ../../../src_llt/sd8911/hidsp0/testcase/alg/odsp_test_tx_fde.hpp:10, from ../../../src_llt/sd8911/hidsp0/testcase/alg/odsp_test_tx_fde.cpp:27: D:/8911/hlt/thirdparty/googletest/googletest-release-1.11.0/googletest/include/gtest/gtest.h:1928: warning: "FAIL" redefined In file included from D:/8911/src/cbb/base/common.h:12, from D:/8911/hlt/src_llt/common/llt_common.hpp:18, from ../../../src_llt/sd8911/hidsp0/testcase/alg/odsp_test_tx_fde.cpp:25: D:/8911/src/cbb/base/error_code.h:144: note: this is the location of the previous definition ../../../src_llt/sd8911/hidsp0/testcase/alg/odsp_test_tx_fde.cpp: In member function 'virtual void OdstTestTxFdeTest_test2_Test::TestBody()': ../../../src_llt/sd8911/hidsp0/testcase/alg/odsp_test_tx_fde.cpp:417:5: error: 'MOCKER' was not declared in this scope compilation terminated due to -Wfatal-errors. ninja: build stopped: subcommand failed. [build] make build end. For more detailed information, please check ./BUILD.LOG. Build Fail! 什么问题
最新发布
07-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值