Pytest: run test from code, not command line

Is it possible to run tests from code using pytest? I found pytest.main

, but it's just a command line interface accessible from code. I would like to pass a test class / function from code.

In unittest it is possible like this:

from unittest import TestLoader, TestCase, TestResult


class TestMy(TestCase):
    def test_silly(self):
        assert False

runner = TestLoader()
test_suite = runner.loadTestsFromTestCase(TestMy)
test_result = TestResult()
test_suite.run(test_result)
print(test_result)

      

+3


source to share





All Articles