如何存储 python Nostests 的测试数据?

发布于 2024-09-19 02:06:13 字数 405 浏览 5 评论 0原文

我想为 python MFCC 特征提取器编写一些测试,以便与nosetest一起运行。除了一些较低级别的测试之外,我还希望能够使用单元测试存储一些标准输入和预期输出文件。

目前,我们正在对服务器上文件的路径进行硬编码,但我希望测试文件(输入和预期输出)位于代码存储库中的某个位置,以便它们可以与测试一起保持在源代码控制之下代码。

我遇到的问题是,我不确定放置测试文件的最佳位置在哪里,以及当 notest 调用每个测试函数时如何知道该路径是什么。目前,我正在考虑将测试数据存储在与测试相同的文件夹中,并使用 __file__ 来确定它在哪里(这可行吗?),但我愿意接受其他建议。

I want to write some tests for a python MFCC feature extractor for running with nosetest. As well as some lower-level tests, I would also like to be able to store some standard input and expected-output files with the unit tests.

At the moment we are hard-coding the paths to the files on our servers, but I would prefer the testing files (both input and expected-output) to be somewhere in the code repository so they can be kept under source control alongside the testing code.

The problem I am having is that I'm not sure where the best place to put the testing files would be, and how to know what that path is when nosetest calls each testing function. At the moment I am thinking of storing the testing data in the same folder as the tests and using __file__ to work out where that is (would that work?), but I am open to other suggestions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁虫ゝ 2024-09-26 02:06:14

我认为使用 __file__ 来确定测试所在的位置并在旁边存储数据是一个好主意。我对我编写的一些测试也做了同样的事情。

这:

os.path.dirname(os.path.abspath(__file__))

可能是你能得到的最好的,而且还不错。 :-)

I think that using __file__ to figure out where the test is located and storing data alongside the it is a good idea. I'm doing the same for some tests that I write.

This:

os.path.dirname(os.path.abspath(__file__))

is probably the best you are going to get, and that's not bad. :-)

神也荒唐 2024-09-26 02:06:14

基于使用 __file__ 的思想,也许您可​​以使用模块来帮助构建路径。您可以找到模块目录中包含的所有文件,将它们的名称和路径收集在字典中以供以后使用。

创建一个可供测试访问的模块,即测试之外的目录,例如 testData,您可以在其中放置数据文件。在此模块的 __init__.py 中,插入以下代码。

import os
from os.path import join,dirname,abspath
testDataFiles = dict()
baseDir = dirname(abspath(__file__)) + os.path.sep 
for root, dirs, files in os.walk(baseDir):
    localDataFiles = [(join(root.replace(baseDir,""),name), join(root,name)) for name in files] 
    testDataFiles.update( dict(localDataFiles))

假设您调用了模块 testData 并且它包含一个名为 data.txt 的文件,那么您可以在测试中使用以下构造来获取该文件的路径。 aFileOperation 被假定为一个采用参数路径的函数,

import unittest
from testData import testDataFiles

class ATestCase(unittest.TestCase):
    def test_Something(self):
        self.assertEqual( 0, aFileOperation(testDataFiles['data.txt'] )

它还允许您使用子目录,例如

    def test_SomethingInASubDir(self):
        self.assertEqual( 0, aFileOperation(testDataFiles['subdir\\data.txt'] )

Based on the idea of using __file__, maybe you could use a module to help with the path construction. You could find all the files contained in the module directory, gather their name and path in a dictionnary for later use.

Create a module accessible to your tests, i.e. a directory besides your test such as testData, where you can put your data files. In the __init__.py of this module, insert the following code.

import os
from os.path import join,dirname,abspath
testDataFiles = dict()
baseDir = dirname(abspath(__file__)) + os.path.sep 
for root, dirs, files in os.walk(baseDir):
    localDataFiles = [(join(root.replace(baseDir,""),name), join(root,name)) for name in files] 
    testDataFiles.update( dict(localDataFiles))

Assuming you called your module testData and it contains a file called data.txt you can then use the following construct in your test to obtain the path to the file. aFileOperation is assumed to be a function that take a parameter path

import unittest
from testData import testDataFiles

class ATestCase(unittest.TestCase):
    def test_Something(self):
        self.assertEqual( 0, aFileOperation(testDataFiles['data.txt'] )

It will also allow you to use subdirectories such as

    def test_SomethingInASubDir(self):
        self.assertEqual( 0, aFileOperation(testDataFiles['subdir\\data.txt'] )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文