0

I have two tests created with pytest: Test1 and Test2. I would like to create method which would start whose two tests. I know that i can do it with cmd, but i prefer py file. In unittest there are testloader and testsuite methods:

t1 = unittest.TestLoader().loadTestsFromTestCase(Test1)
t2 = unittest.TestLoader().loadTestsFromTestCase(Test2)
test_suite = unittest.TestSuite([t1, t2])
unittest.TextTestRunner(verbosity=2).run(test_suite)

I need same thing, but for py test

NiPa
  • 93
  • 1
  • 14
  • In pytest it's going to be easier to just [add markers](https://docs.pytest.org/en/latest/example/markers.html), and run the marked tests with `-m`. – Lie Ryan Feb 20 '19 at 15:23
  • Okay, i marked my both tests @pytest.mark.test_suite How can i call this suite from py file? Not from console – NiPa Feb 20 '19 at 15:25
  • Run with `pytest -m test_suite`. – Lie Ryan Feb 20 '19 at 15:28
  • I think that we don`t understand each other. This is console command "pytest -m test_suite" http://joxi.ru/l2ZOpbGHwb7jaA i am getting SyntaxError: invalid syntax – NiPa Feb 20 '19 at 15:42
  • 1
    [`import pytest; pytest.main(args=['-m', 'test_suite'])`](https://docs.pytest.org/en/latest/reference.html#pytest-main) – hoefling Feb 20 '19 at 19:39

1 Answers1

2

You can use pytest.main() at the end of the test file. You can also pass arguments as list: `pytest.main(['-x', 'mytestdir']).

Documentation: https://docs.pytest.org/en/latest/usage.html#calling-pytest-from-python-code

SilentGuy
  • 1,867
  • 17
  • 27