脚本中的python鼻子,从文件中收集测试类,然后运行测试 [英] python nose from a script, gathers test classes from files and then runs tests

查看:109
本文介绍了脚本中的python鼻子,从文件中收集测试类,然后运行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何使用python脚本中的鼻子来

How would I use nose from a python script to

  1. 从目录中收集python文件
  2. foreach文件
  1. gather python files from a directory
  2. foreach file
  1. 运行使用传递的参数找到的所有测试类

这是给定文件的示例

/run.py
/tests/TestClassA.py

并且在 TestClassA.py 中是代码

  class A():
     __init__(self, b):
          self._b = b
     test_run():
          print("%s",self._b)



重述需求:
我想从run.py打电话给鼻子.我想要鼻子(或鼻子的一部分)



To restate the need:
I want to call nose from run.py. I want nose (or some part of nose) to

  1. 在文件TestClassA.py
  2. 中找到类 A
  3. 创建名为 a A 实例,并将字符串"foo" 传递给 A .__初始化__ 功能
  4. 致电 a.test_run()
  1. find class A in file TestClassA.py
  2. create an instance of A, named a, passing the string "foo" to A.__ init __ function
  3. call a.test_run()

该请求中 run.py 中的python鼻子代码是什么?
如果不是python鼻子,python unittests会做得更好吗?

What is the python nose code within run.py for this request?
If not python nose , would python unittests do any better?

推荐答案

在run.py中:

import nose
result = nose.run()

您可以通过传递run()调用适当的参数来选择要运行的测试,请参见鼻子的使用选项.鼻子会发现TestClassA.py很好,因为文件名以test开头.通常,您只需要传递项目的根路径即可.

You select which tests to run by passing the run() call the appropriate arguments, see the usage options for nose. Nose will find TestClassA.py just fine as the filename starts with test. You just have to pass the root path of your project, generally.

您可能应该阅读有关实例化要在测试中使用的对象的文档.如果您确实想像编写代码那样进行操作,则可以编写一个创建A对象并运行该测试的单元测试,但是这样做会破坏使用鼻子的目的-通常,您测试的不是一个单独定义的对象在定义测试的代码中.

You should probably read the nose docs about instantiating objects to use in your tests. If you really want to do it like you've written, you could write a unit test that creates an A object and runs the test, but that kind of defeats the purpose of using nose - Normally you test something that's not solely defined in the code defining the test.

这篇关于脚本中的python鼻子,从文件中收集测试类,然后运行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆