tk0miya / testing.postgresql

Apache License 2.0
290 stars 46 forks source link

Cannot run tests with testing.postgresql - application is not being found and failes to shutdown the server automatically #42

Open krzysztofcyran93 opened 2 years ago

krzysztofcyran93 commented 2 years ago

Hello, the below appears when following the guide:

ERROR: tearDownModule (__main__)
----------------------------------------------------------------------
TypeError: tearDownModule() missing 1 required positional argument: 'self'

I'm not quite sure how is this supposed to work, since the mehod is outside of the TestCase class

import unittest
import testing.postgresql

def tearDownModule(self):
    Postgresql.clear_cache()

class Base(unittest.TestCase):

    def setUp(self) -> None:
        self.postgresql = Postgresql()
        print(self.postgresql.url())

    def tearDown(self) -> None:
        self.postgresql.stop()

class TestInitial(Base):
    def some_test_suite(self):
        print('Test this works')

def some_test_suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.TestLoader().loadTestsFromTestCase(TestInitial))
    return suite

if __name__ == '__main__':
    with testing.postgresql.Postgresql() as postgresql:
        engine = create_engine(postgresql.url())
        db = psycopg2.connect(**postgresql.dsn())
        runner = unittest.TextTestRunner()
        runner.run(some_test_suite())

Next what happens is two things:

No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/.

and

ERROR: testing.common.database: failed to shutdown the server automatically. Any server processes and files might have been leaked. Please remove them and call the stop() certainly

Full log:

postgresql://postgres@127.0.0.1:47359/test
No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/.
.
----------------------------------------------------------------------
Ran 1 test in 0.807s

OK
ERROR: testing.common.database: failed to shutdown the server automatically.
Any server processes and files might have been leaked. Please remove them and call the stop() certainly
ERROR: testing.common.database: failed to shutdown the server automatically.
Any server processes and files might have been leaked. Please remove them and call the stop() certainly

I've been struggling with this for quite a while and I will appreciate any feedback. Thank you