如何在生产脚本和测试脚本中为变量分配不同的值,

时间:2019-01-09 22:47:40

标签: python unit-testing mocking

此函数创建一个目录,我们依靠模式确定脚本是生产代码还是测试文件。创建测试文件时,我 def setup():

  

Flags.is_test = True(这也不应该使用

有一种方法: 消除对FLAGS.is_test的所有引用。代替,  #将mode定义为模块属性,并使这些函数成为类方法  使用模块属性。 稍后,当您使用此模块编写应用程序时,如果  生产中,为模式注入值1111。如果发展,则注入 值1234。

_CreateDirectory(path, is_dir=False):

  if FLAGS.is_test:
    mode = 1234
  else:
    mode = 1111

  if '/private_file_location/' in path:
    if not Modulefile.Exists(directory_name):
      _CreateDirectory(directory_name, True)
    if not file.Exists(path) and is_dir:
      Modulefile.MkDir(path, mode=mode)
      Moudlefile.SetMode(path, mode)
  else:
    if not os.path.exists(directory_name):
      _CreateDirectory(directory_name, True)
    if not os.path.exists(path) and is_dir:
      os.mkdir(path, mode)
      os.chmod(path, mode)

有人可以替我重写脚本吗? 谢谢

0 个答案:

没有答案