简体   繁体   English

Python单元测试,测试用例之前的声明

[英]Python unittests, statement before test cases

Imagine I have tests like that: 想象一下,我有这样的测试:

import unittest

class MyTests(unittest.TestCase):

  print("Starting")

  def test_first(self):
    .....

Is the print statement guaranteed to be executed before test_first() and the rest? 是否保证在test_first()和其余部分之前执行print语句? From what I've seen it does get executed first, but are there any edge cases? 从我所看到的它首先被执行,但是有没有边缘情况?

You can use setUp() ( docs ) and setUpClass() ( docs ) methods for this. 您可以使用setUp()docs )和setUpClass()docs )方法。 The setUp() method is executed before each individual test while the setUpClass() method is executed before all the tests in this class run. setUp()方法在每次单独测试之前执行,而setUpClass()方法在此类中的所有测试运行之前执行。

import unittest

class MyTests(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        print("Starting all the tests.")

    def setUp():
        print("Starting another test.")

    def test_first(self):
        .....

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

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