0

I have a BLE device. I want to connect to it, run all my tests then deconnect. Here is what versions I'm using: bleak: "0.22.2"; bleak-winrt: "1.2.0"; python_full_version: "3.11.9"

class MyTest(unittest.IsolatedAsyncioTestCase):

    async def asyncSetUp(self):
        await super().asyncSetUp()
        self.device = Device()
        self.assertTrue(await self.device.connect())

    async def asyncTearDown(self):
        await super().asyncTearDown()
        await self.device.disconnect()

    async def test_a(self):
    async def test_b(self):
    async def test_c(self):

I want to use the benefit of the bluetooth low energy and not pair with the device.

Here is my problem, asyncSetUp and asyncTearDown are methods that are called before and after each test. So the sequence right now is: connect, run test_a, deconnect; connect, test_b, deconnect... Which is really inefficient.
I would like something like that: connect, run test_a test_b...; deconnect.

I need something like setUpClass/tearDownClass (or even better something like setUpModule), but the problem is that they are not async, however, device.connect is async and needs to be awaited.

Is there a known solution for this problem?

2
  • 1
    My advice is to switch to Pytest instead. docs.pytest.org/en/stable It has a number of features that Unittest is lacking. It can also execute Unittest tests, so no need to convert all. Here's how to do async tests in Pytest: stackoverflow.com/questions/70015634/… Not sure if it solves your problem though, but might be worth a look? Commented Aug 12, 2024 at 11:27
  • Thanks for the advice, I'm gonna look into it. Commented Aug 12, 2024 at 11:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.