简体   繁体   English

Python单元测试:为模拟输入设计目录结构吗?

[英]Python Unittesting: Design directory structure for mock input?

I am writing production code that parses a large file, which itself contains many different cases. 我正在编写用于解析大文件的生产代码,该文件本身包含许多不同的情况。 To unittest the production code, I want to write a test case for each case. 为了对生产代码进行单元测试,我想为每个案例编写一个测试案例。 My question case is what a good way is to set up the testing directory and the testing code. 我的问题是设置测试目录和测试代码的好方法是什么。

The current directory structure is: 当前目录结构为:

root/
    src/
        parser.py
    test/
        test_parser.py
        raw/
            case_1.csv
            case_2.csv
            ...

The testing code looks like this: 测试代码如下:

from parser import parse
import unittest import TestCase


class Test_Parser(TestCase):

   def setUp(self):
       raw_dir = 'raw'

   def test_case_1(self):
       filename = self.raw_dir + '/case_1.csv'
       actual = parse(filename)
       # assert actual == expected

   def test_case_2(self):
       filename = self.raw_dir + '/case_2.csv'
       actual = parser(filename)
       # assert actual == expected

   ...

Is there a more Pythonic way to factor the directory self.raw_dir out, so that the test case is cleaner and one only has to write filename = 'case_1.csv' ? 有没有一种更Python化的方法可以将目录self.raw_dir出来,从而使测试用例更加整洁,并且只需要写入filename = 'case_1.csv'

If you want to avoid writing the whole path in your test methods, which is understandable, you could isolate this in a separate method which doesn't begin with _test and therefore won't be called when executing the tests: 如果您想避免在测试方法中编写整个路径,这是可以理解的,则可以将其隔离在一个不以_test开头的单独方法中,因此在执行测试时不会被调用:

import os
import parser
import unittest

class TestParser(unittest.TestCase):

    def setUp(self):
        self.raw_dir = "raw"

    def get_path(self, filename):
        path = os.path.join(self.raw_dir, filename)
        return path

    def file_parse(self, filename):
        # every method of this class being supposed to call
        # the parser, it makes sense to isolate these calls
        # into a specific method to avoid repetition
        # - won't be the same is the class is more general-purpose
        path = self.get_path(filename)
        actual = parser.parse(path)
        return actual

    def test_case_1(self):
        actual = self.file_parse(filename='case_1.csv')
        # assert actual == expected

    def test_case_2(self):
        actual = self.file_parse(filename='case_2.csv')
        # assert actual == expected

The main idea here is separation of concerns . 这里的主要思想是关注点分离 One specific method to take care of the "putting dirs and filenames into a correct path" labour, another one to do the parsing and the test methods to do the actual tests (and only that). 一种用于“将目录和文件名放入正确路径”的特殊方法,另一种用于进行解析,测试方法用于进行实际测试(仅此而已)。

Note that as suggested, you should use os.path.join ( documentation ) instead of hardcoding the slashes / . 请注意,如建议的那样,您应该使用os.path.joindocumentation )而不是对斜杠/进行硬编码。

Also, according to PEP8 , class names should be CamelCase , not under_score or A_Mix_Of_Both . 而且,根据PEP8类名应该是 CamelCase ,而不是under_scoreA_Mix_Of_Both

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM