Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytest: running in threading

A python file named pytest_demo.py:

import pytest
import threading

@pytest.mark.test
class TestDemo():
    def test_demo_false(self):
        assert False

    def test_demo_true(self):
        assert True

    def test_demo_thread_true(self):
        thread1 = MyThread(True)
        thread1.start()

    def test_demo_thread_false(self):
        thread1 = MyThread(False)
        thread1.start()


class MyThread(threading.Thread):
    def __init__(self, flag):
        threading.Thread.__init__(self)
        self.flag = flag

    def run(self):
        print "Starting "
        assert self.flag


if __name__ == "__main__":
    pytest.main(['-v', '-m', 'test', 'pytest_demo.py'])

The output is after run " python pytest_demo.py":

pytest_demo.py:8: TestDemo.test_demo_false FAILED
pytest_demo.py:11: TestDemo.test_demo_true PASSED
pytest_demo.py:14: TestDemo.test_demo_thread_true PASSED
pytest_demo.py:18: TestDemo.test_demo_thread_false PASSED

in thread, why the TestDemo.test_demo_thread_false is PASSED?

like image 283
Lei Gao Avatar asked Apr 28 '14 10:04

Lei Gao


People also ask

Is pytest multithreaded?

This plugin makes it possible to run tests quickly using multiprocessing (parallelism) and multithreading (concurrency).

Does pytest run tests in parallel?

Learn Pytest From Scratch By default, pytest runs tests in sequential order. In a real scenario, a test suite will have a number of test files and each file will have a bunch of tests. This will lead to a large execution time. To overcome this, pytest provides us with an option to run tests in parallel.

Does pytest run tests sequentially?

pytest-ordering is a pytest plugin to run your tests in any order that you specify. It provides custom markers that say when your tests should run in relation to each other. They can be absolute (i.e. first, or second-to-last) or relative (i.e. run this test before this other test).

Is pytest a test runner?

Pytest PyCharm supports pytest, a fully functional testing framework. The following features are available: The dedicated test runner.

How to start and stop a thread in Python?

Start and stop a thread in Python. The threading library can be used to execute any Python callable in its own thread. To do this, create a Thread instance and supply the callable that you wish to execute as a target as shown in the code given below –. When a thread instance is created, it doesn’t start executing until its start () ...

Why do we use threads in Python?

Sometimes it is easier to use threads and have them cook the dishes in the same kitchen, even if that means they have to read from the same recipe book. The GIL means that only one thread can read the Python code at once. This means multiple threads can lock each other out.

How to execute a callable in its own thread in Python?

The threading library can be used to execute any Python callable in its own thread. To do this, create a Thread instance and supply the callable that you wish to execute as a target as shown in the code given below –

How to get the slowest 10 test durations in pytest?

To get a list of the slowest 10 test durations over 1.0s long: By default, pytest will not show test durations that are too small (<0.005s) unless -vv is passed on the command-line. New in version 5.0. The faulthandler standard module can be used to dump Python tracebacks on a segfault or after a timeout.


Video Answer


1 Answers

Because the AssertionError gets raised in a separate thread. Your test_demo_thread_false method doesn't assert anything, it just spawns a new thread, and it always does that successfully.

like image 152
radu.ciorba Avatar answered Nov 09 '22 14:11

radu.ciorba