V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
SlipStupig
V2EX  ›  Python

Python unitest 怎么按顺序执行

  •  
  •   SlipStupig · 2017-05-10 13:44:49 +08:00 · 4047 次点击
    这是一个创建于 2540 天前的主题,其中的信息可能已经有所发展或是发生改变。
    我发现不是按顺序执行的,怎么才能按顺序执行呢?
    16 条回复    2017-05-12 19:25:34 +08:00
    zjq426
        1
    zjq426  
       2017-05-10 14:00:17 +08:00   ❤️ 1
    TestLoader 有一个 sortTestMethodsUsing 方法,给一个新的 sorting function 可以覆盖默认的按测试名称排序的方法,也可以直接设为 None 关闭排序。如果测试有一些先决条件请参考 test 的 setup 部分。
    Kilerd
        2
    Kilerd  
       2017-05-10 16:26:35 +08:00 via iPhone
    测试应该是独立的吧,顺序不影响啊
    SlipStupig
        3
    SlipStupig  
    OP
       2017-05-10 17:01:07 +08:00
    @Kilerd
    class TestCase():

    def one(self):
    put()

    def two(self):
    get()
    我想先执行 1 才能执行 2
    binux
        4
    binux  
       2017-05-10 17:02:35 +08:00   ❤️ 1
    @SlipStupig #3 test_1_one, test_2_two
    fy
        5
    fy  
       2017-05-10 17:05:15 +08:00
    感觉有点僵硬
    SlipStupig
        6
    SlipStupig  
    OP
       2017-05-10 17:12:17 +08:00
    @binux OK
    SlipStupig
        7
    SlipStupig  
    OP
       2017-05-10 17:14:42 +08:00
    @fy ????
    Lycnir
        8
    Lycnir  
       2017-05-10 21:57:46 +08:00
    最近我们也在写测试,不过是投入 pytest 的怀抱
    SlipStupig
        9
    SlipStupig  
    OP
       2017-05-11 07:03:35 +08:00
    @Lycnir 我想用 nose 但是感觉跟 unitest 区别不大
    fy
        10
    fy  
       2017-05-11 11:12:12 +08:00
    @SlipStupig 我是说不能调顺序挺僵硬的
    andy1990038
        11
    andy1990038  
       2017-05-11 13:58:03 +08:00
    根据我上回的发现,这个顺序是按测试方法的首字母顺序来的...
    julyclyde
        12
    julyclyde  
       2017-05-11 22:53:49 +08:00
    有这个需求基本上意味着你错了
    SlipStupig
        13
    SlipStupig  
    OP
       2017-05-11 23:04:05 +08:00
    @julyclyde 测试不应该有前置条件?
    264768502
        14
    264768502  
       2017-05-12 07:48:30 +08:00 via Android   ❤️ 1
    一般情况下前置条件用 setUp,而不是用另一个 test
    julyclyde
        15
    julyclyde  
       2017-05-12 15:35:06 +08:00   ❤️ 1
    @SlipStupig 单元测试啊,单元就是边界
    SlipStupig
        16
    SlipStupig  
    OP
       2017-05-12 19:25:34 +08:00
    @264768502 感谢指点
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3148 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 14:31 · PVG 22:31 · LAX 07:31 · JFK 10:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.