【问题标题】:Is it possible to invoke the main method of a test class through pytest?是否可以通过pytest调用测试类的main方法?
【发布时间】:2017-08-12 14:41:51
【问题描述】:

我有一个测试类test_enrollment.py,它定义了一组测试,并且有一个使用生成器生成新的自定义测试类的 main 方法。在更简单的形式中,它看起来像:

import json
import unittest

import front_end_tests.generator


class EnrollmentTests(unittest.TestCase):

    def test_new_student_registration(self):
        # test definition here

if __name__ == '__main__':
    front_end_tests.generator.generate_and_run_tests(EnrollmentTests)

我通常运行这个测试类的方式是调用python test_enrollment.py。如果我想用pytest 运行同一个测试文件,有没有办法以仍然调用主要方法的方式调用文件?

【问题讨论】:

    标签: python appium pytest python-unittest


    【解决方案1】:

    我以为你不能。 要解决这个问题,你只需要移动

    front_end_tests.generator.generate_and_run_tests(EnrollmentTests)
    

    到

    def setUpModule():
        front_end_tests.generator.generate_and_run_tests(EnrollmentTests)
    

    test_enrollment.py 看起来像这样:

    import json
    import unittest
    
    import front_end_tests.generator
    
    def setUpModule():
        front_end_tests.generator.generate_and_run_tests(EnrollmentTests)
    
    class EnrollmentTests(unittest.TestCase):
    
        def test_new_student_registration(self):
            # test definition here
            pass
    
    if __name__ == '__main__':
        unittest.main()
    

    然后输入:

    pytest
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-17
      相关资源
      最近更新 更多