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
python python-3.x unit-testing py.test


source to share


No one has answered this question yet

Check out similar questions:

1675
Why is reading lines from stdin much slower in C ++ than Python?
830
Unit Testing C Code
646
How do I unit test threaded code?
623
Unittest execution with typical test directory structure
78
How do I configure PyCharm to run py.test tests?
25
Is there a way to control how pytest-xdist runs tests in parallel?
2
With pytest, why don't inherited test methods give correct output result?
2
Python pytest unittest.TextTestRunner does not run certain packages
0
Error while testing pydev test: imported unittest before running pytest.main
0
Can't get testname when calling pytest execution from python or subprocess



All Articles
Loading...
X
Show
Funny
Dev
Pics