无样板试验

2024-09-29 23:29:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有Python中的测试文件wirtten:test1.py test2.py。。。 在执行它们之前,我需要用一个名为初始化.py这需要争论。你知道吗

测试文件必须尽可能地轻和易于编写。你知道吗

我想创建一个脚本:

  1. 接受输入参数
  2. 启动初始化.py用这些参数归档
  3. 使用创建的变量启动测试文件初始化.py你知道吗

我想了几个办法:

  • 导入两个文件:这不起作用,因为使用Import可以在主脚本上使用return参数,但不能提供input参数
  • 将两个文件转换为函数:这不是问题初始化.py但正如我所说的,我希望保持测试文件尽可能的简单和轻巧,这样如果我能避免这样做就更好了。你知道吗
  • 完美的解决方案是简单地从initialize中“复制”代码,并将其放入测试文件的beging中(或相反)。可能是创建一个临时文件包含这两个代码,但我不认为它很干净。你知道吗

总而言之:这就好像我有100个文件以相同的25行开头,我想把这25行放在一个文件中,每次导入它们。你知道吗

另一种方式是查看3个文件:

#File1.py
var1 = sys.argv(1)

#File2.py
var2 = var1+"second"

#File3.py
var3 = var1+var2+"third"
print var3

我想先开始./File1.py 得到“第一秒第三”

我成功了

#! /usr/bin/python
import sys

import subprocess
source_content = "#! /usr/bin/python\n"+"import sys\n"

sourcefile = "file2.py"
txt_file = open(sourcefile)
source_content += txt_file.read()

sourcefile = "file3.py"
txt_file = open(sourcefile)
source_content += txt_file.read()

destinationfile = "Copyfile2.py"
target = open (destinationfile, 'w')
target.write(source_content)

target.close()

chmodFile = "chmod 777 "+destinationfile
chmod = subprocess.Popen(chmodFile, shell=True)
chmod.wait()
arguments = str("./"+destinationfile+" ")
arguments += " ".join(map(str,sys.argv[1:len(sys.argv)]))
startTest = subprocess.Popen(arguments, shell=True)
startTest.wait()

但我不得不删除“#”!/usr/bin/python”,并将var1测试并重命名为系统参数[1] 在这些文件上。 我觉得这不是个好办法。。。你知道吗


Tags: 文件pyimporttxtsource参数binusr
1条回答
网友
1楼 · 发布于 2024-09-29 23:29:25

你用unittest模块怎么样?你知道吗

import unittest

class BaseTest(unittest.TestCase):
    def initialisation_script_stuff(blah,etc):
        foo

    def setUp(self):
        common_setup_stuff()

    def tearDown(self):
        whatever

现在您可以在每个测试文件中继承BaseTest。你知道吗

from moo import BaseTest

class CoolTest(BaseTest):
    def setUp(self):
        BaseTest.setUp(self)
        args = BaseTest.initialisation_script_stuff()
        do_stuff(args)

    def testNumberOne(self):
        self.assertEqual(1,1)

或者,如果你想远离标准的单元测试方法。。。你知道吗

假设目录结构:

all_tests\
    __init__.py
    some_tests\
        __init__.py
        test1.py
        test2.py
    other _tests\
        __init__.py
        etc

一些命名约定:

  • 每个测试py文件都有一个名为run的函数来运行测试
  • 每个测试文件都有一个以“test”开头的名称
  • 每个测试分组文件夹都有一个以“\u tests”结尾的名称

生成一个名为run的脚本_测试.py(或者类似的…)

def run_tests():
    import os
    import importlib
    import re
    dTests = {}
    lFolders = [s for s in os.listdir('all_tests') if re.match('.*_tests$',s)]
    for sFolder in lFolders:
        sFolderPath = os.path.join('all_tests',sFolder)
        lTestFileNames = [s for s in os.listdir(sFolderPath) if re.match('^test.*py$',s)]
        for sFileName in lTestFileNames:        
            sSubPath = '{0}.{1}'.format(sFolder,sFileName.split('.')[0])
            dTests[sSubPath] = importlib.import_module('all_tests.{0}'.format(sSubPath))
    #now you have all the tests...
    for k in dTests:
        stuff = initialisation_stuff()
        test_result = dTests[k].run(stuff)
        do_whatever_you_want(test_result)             

if __name__ == "__main__":
    run_tests()

现在您的测试文件中完全不需要锅炉板代码。只要你遵循这个公式

相关问题 更多 >

    热门问题