Fail to run multiple tests at once on Pycharm

I used to be able to run multi unit tests at once by clicking on the run button on the left side of my test class or right click in my test file and select “Run pytest in test_….”. But recently I got error when doing this. In fact, I can only run single test now. If I try to run multi tests at once, all the tests fail except the first one. 

The error is as below:

============================= test session starts ==============================
collecting ... collected 3 items

test_data_sources.py::TestDataSourceTest::test_data_source_test_fail 
test_data_sources.py::TestDataSourceTest::test_data_source_test_success 
test_data_sources.py::TestDataSourceTest::test_data_source_test_timeout 

=================== 2 failed, 1 passed, 55 warnings in 3.75s ===================
PASSED [ 33%]FAILED [ 66%]
tests/handlers/test_data_sources.py:292 (TestDataSourceTest.test_data_source_test_success)
self = <sqlalchemy.engine.base.Connection object at 0x13f64de80>
dialect = <sqlalchemy.dialects.postgresql.psycopg2.PGDialect_psycopg2 object at 0x13f453490>
constructor = <bound method DefaultExecutionContext._init_statement of <class 'sqlalchemy.dialects.postgresql.psycopg2.PGExecutionContext_psycopg2'>>
statement = "CREATE FUNCTION\n                queries_search_vector_update() RETURNS TRIGGER AS $$\n            BEGIN\n           ...')), %(setweight_4)s);\n                RETURN NEW;\n            END\n            $$ LANGUAGE 'plpgsql';\n            "
parameters = {'setweight_1': 'B', 'setweight_2': 'A', 'setweight_3': 'C', 'setweight_4': 'D', ...}
args = ("CREATE FUNCTION\n                queries_search_vector_update() RETURNS TRIGGER AS $$\n            BEGIN\n          ...AGE 'plpgsql';\n            ", [{'setweight_1': 'B', 'setweight_2': 'A', 'setweight_3': 'C', 'setweight_4': 'D', ...}])
conn = <sqlalchemy.pool.base._ConnectionFairy object at 0x13f435550>
context = <sqlalchemy.dialects.postgresql.psycopg2.PGExecutionContext_psycopg2 object at 0x13f69c460>

    def _execute_context(
        self, dialect, constructor, statement, parameters, *args
    ):
        """Create an :class:`.ExecutionContext` and execute, returning
        a :class:`.ResultProxy`."""
    
        try:
            try:
                conn = self.__connection
            except AttributeError:
                # escape "except AttributeError" before revalidating
                # to prevent misleading stacktraces in Py3K
                conn = None
            if conn is None:
                conn = self._revalidate_connection()
    
            context = constructor(dialect, self, conn, *args)
        except BaseException as e:
            self._handle_dbapi_exception(
                e, util.text_type(statement), parameters, None, None
            )
    
        if context.compiled:
            context.pre_exec()
    
        cursor, statement, parameters = (
            context.cursor,
            context.statement,
            context.parameters,
        )
    
        if not context.executemany:
            parameters = parameters[0]
    
        if self._has_events or self.engine._has_events:
            for fn in self.dispatch.before_cursor_execute:
                statement, parameters = fn(
                    self,
                    cursor,
                    statement,
                    parameters,
                    context,
                    context.executemany,
                )
    
        if self._echo:
            self.engine.logger.info(statement)
            if not self.engine.hide_parameters:
                self.engine.logger.info(
                    "%r", sql_util._repr_params(parameters, batches=10)
                )
            else:
                self.engine.logger.info(
                    "[SQL parameters hidden due to hide_parameters=True]"
                )
    
        evt_handled = False
        try:
            if context.executemany:
                if self.dialect._has_events:
                    for fn in self.dialect.dispatch.do_executemany:
                        if fn(cursor, statement, parameters, context):
                            evt_handled = True
                            break
                if not evt_handled:
                    self.dialect.do_executemany(
                        cursor, statement, parameters, context
                    )
            elif not parameters and context.no_parameters:
                if self.dialect._has_events:
                    for fn in self.dialect.dispatch.do_execute_no_params:
                        if fn(cursor, statement, context):
                            evt_handled = True
                            break
                if not evt_handled:
                    self.dialect.do_execute_no_params(
                        cursor, statement, context
                    )
            else:
                if self.dialect._has_events:
                    for fn in self.dialect.dispatch.do_execute:
                        if fn(cursor, statement, parameters, context):
                            evt_handled = True
                            break
                if not evt_handled:
>                   self.dialect.do_execute(
                        cursor, statement, parameters, context
                    )

../../venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py:1248: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <sqlalchemy.dialects.postgresql.psycopg2.PGDialect_psycopg2 object at 0x13f453490>
cursor = <cursor object at 0x13eddb9a0; closed: -1>
statement = "CREATE FUNCTION\n                queries_search_vector_update() RETURNS TRIGGER AS $$\n            BEGIN\n           ...')), %(setweight_4)s);\n                RETURN NEW;\n            END\n            $$ LANGUAGE 'plpgsql';\n            "
parameters = {'setweight_1': 'B', 'setweight_2': 'A', 'setweight_3': 'C', 'setweight_4': 'D', ...}
context = <sqlalchemy.dialects.postgresql.psycopg2.PGExecutionContext_psycopg2 object at 0x13f69c460>

    def do_execute(self, cursor, statement, parameters, context=None):
>       cursor.execute(statement, parameters)
E       psycopg2.errors.UndefinedFile: could not access file "$libdir/plpgsql": No such file or directory

../../venv/lib/python3.9/site-packages/sqlalchemy/engine/default.py:580: UndefinedFile

The above exception was the direct cause of the following exception:
../__init__.py:65: in setUp
    db.create_all()
../../venv/lib/python3.9/site-packages/flask_sqlalchemy/__init__.py:1039: in create_all
    self._execute_for_all_tables(app, bind, 'create_all')
../../venv/lib/python3.9/site-packages/flask_sqlalchemy/__init__.py:1031: in _execute_for_all_tables
    op(bind=self.get_engine(app, bind), **extra)
../../venv/lib/python3.9/site-packages/sqlalchemy/sql/schema.py:4303: in create_all
    bind._run_visitor(
../../venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py:2046: in _run_visitor
    conn._run_visitor(visitorcallable, element, **kwargs)
../../venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py:1615: in _run_visitor
    visitorcallable(self.dialect, self, **kwargs).traverse_single(element)
../../venv/lib/python3.9/site-packages/sqlalchemy/sql/visitors.py:138: in traverse_single
    return meth(obj, **kw)
../../venv/lib/python3.9/site-packages/sqlalchemy/sql/ddl.py:777: in visit_metadata
    self.traverse_single(
../../venv/lib/python3.9/site-packages/sqlalchemy/sql/visitors.py:138: in traverse_single
    return meth(obj, **kw)
../../venv/lib/python3.9/site-packages/sqlalchemy/sql/ddl.py:843: in visit_table
    table.dispatch.after_create(
../../venv/lib/python3.9/site-packages/sqlalchemy/event/attr.py:322: in __call__
    fn(*args, **kw)
../../venv/lib/python3.9/site-packages/sqlalchemy_searchable/__init__.py:294: in after_create
    connection.execute(str(clause), **clause.params)
../../venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py:982: in execute
    return self._execute_text(object_, multiparams, params)
../../venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py:1149: in _execute_text
    ret = self._execute_context(
../../venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py:1252: in _execute_context
    self._handle_dbapi_exception(
../../venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py:1473: in _handle_dbapi_exception
    util.raise_from_cause(sqlalchemy_exception, exc_info)
../../venv/lib/python3.9/site-packages/sqlalchemy/util/compat.py:398: in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
../../venv/lib/python3.9/site-packages/sqlalchemy/util/compat.py:152: in reraise
    raise value.with_traceback(tb)
../../venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py:1248: in _execute_context
    self.dialect.do_execute(
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <sqlalchemy.dialects.postgresql.psycopg2.PGDialect_psycopg2 object at 0x13f453490>
cursor = <cursor object at 0x13eddb9a0; closed: -1>
statement = "CREATE FUNCTION\n                queries_search_vector_update() RETURNS TRIGGER AS $$\n            BEGIN\n           ...')), %(setweight_4)s);\n                RETURN NEW;\n            END\n            $$ LANGUAGE 'plpgsql';\n            "
parameters = {'setweight_1': 'B', 'setweight_2': 'A', 'setweight_3': 'C', 'setweight_4': 'D', ...}
context = <sqlalchemy.dialects.postgresql.psycopg2.PGExecutionContext_psycopg2 object at 0x13f69c460>

    def do_execute(self, cursor, statement, parameters, context=None):
>       cursor.execute(statement, parameters)
E       sqlalchemy.exc.OperationalError: (psycopg2.errors.UndefinedFile) could not access file "$libdir/plpgsql": No such file or directory
E       
E       [SQL: CREATE FUNCTION
E                       queries_search_vector_update() RETURNS TRIGGER AS $$
E                   BEGIN
E                       NEW.search_vector = ((setweight(to_tsvector(%(to_tsvector_1)s, regexp_replace(coalesce(CAST(NEW.id AS TEXT), ''), '[-@.]', ' ', 'g')), %(setweight_1)s) || setweight(to_tsvector(%(to_tsvector_2)s, regexp_replace(coalesce(NEW.name, ''), '[-@.]', ' ', 'g')), %(setweight_2)s)) || setweight(to_tsvector(%(to_tsvector_3)s, regexp_replace(coalesce(NEW.description, ''), '[-@.]', ' ', 'g')), %(setweight_3)s)) || setweight(to_tsvector(%(to_tsvector_4)s, regexp_replace(coalesce(NEW.query, ''), '[-@.]', ' ', 'g')), %(setweight_4)s);
E                       RETURN NEW;
E                   END
E                   $$ LANGUAGE 'plpgsql';
E                   ]
E       [parameters: {'to_tsvector_1': 'pg_catalog.simple', 'setweight_1': 'B', 'to_tsvector_2': 'pg_catalog.simple', 'setweight_2': 'A', 'to_tsvector_3': 'pg_catalog.simple', 'setweight_3': 'C', 'to_tsvector_4': 'pg_catalog.simple', 'setweight_4': 'D'}]
E       (Background on this error at: http://sqlalche.me/e/e3q8)

../../venv/lib/python3.9/site-packages/sqlalchemy/engine/default.py:580: OperationalError
FAILED [100%]
tests/handlers/test_data_sources.py:309 (TestDataSourceTest.test_data_source_test_timeout)
self = <sqlalchemy.engine.base.Connection object at 0x13f734a30>
dialect = <sqlalchemy.dialects.postgresql.psycopg2.PGDialect_psycopg2 object at 0x13f674ca0>
constructor = <bound method DefaultExecutionContext._init_statement of <class 'sqlalchemy.dialects.postgresql.psycopg2.PGExecutionContext_psycopg2'>>
statement = "CREATE FUNCTION\n                queries_search_vector_update() RETURNS TRIGGER AS $$\n            BEGIN\n           ...')), %(setweight_4)s);\n                RETURN NEW;\n            END\n            $$ LANGUAGE 'plpgsql';\n            "
parameters = {'setweight_1': 'B', 'setweight_2': 'A', 'setweight_3': 'C', 'setweight_4': 'D', ...}
args = ("CREATE FUNCTION\n                queries_search_vector_update() RETURNS TRIGGER AS $$\n            BEGIN\n          ...AGE 'plpgsql';\n            ", [{'setweight_1': 'B', 'setweight_2': 'A', 'setweight_3': 'C', 'setweight_4': 'D', ...}])
conn = <sqlalchemy.pool.base._ConnectionFairy object at 0x13f7349a0>
context = <sqlalchemy.dialects.postgresql.psycopg2.PGExecutionContext_psycopg2 object at 0x13f734fd0>

    def _execute_context(
        self, dialect, constructor, statement, parameters, *args
    ):
        """Create an :class:`.ExecutionContext` and execute, returning
        a :class:`.ResultProxy`."""
    
        try:
            try:
                conn = self.__connection
            except AttributeError:
                # escape "except AttributeError" before revalidating
                # to prevent misleading stacktraces in Py3K
                conn = None
            if conn is None:
                conn = self._revalidate_connection()
    
            context = constructor(dialect, self, conn, *args)
        except BaseException as e:
            self._handle_dbapi_exception(
                e, util.text_type(statement), parameters, None, None
            )
    
        if context.compiled:
            context.pre_exec()
    
        cursor, statement, parameters = (
            context.cursor,
            context.statement,
            context.parameters,
        )
    
        if not context.executemany:
            parameters = parameters[0]
    
        if self._has_events or self.engine._has_events:
            for fn in self.dispatch.before_cursor_execute:
                statement, parameters = fn(
                    self,
                    cursor,
                    statement,
                    parameters,
                    context,
                    context.executemany,
                )
    
        if self._echo:
            self.engine.logger.info(statement)
            if not self.engine.hide_parameters:
                self.engine.logger.info(
                    "%r", sql_util._repr_params(parameters, batches=10)
                )
            else:
                self.engine.logger.info(
                    "[SQL parameters hidden due to hide_parameters=True]"
                )
    
        evt_handled = False
        try:
            if context.executemany:
                if self.dialect._has_events:
                    for fn in self.dialect.dispatch.do_executemany:
                        if fn(cursor, statement, parameters, context):
                            evt_handled = True
                            break
                if not evt_handled:
                    self.dialect.do_executemany(
                        cursor, statement, parameters, context
                    )
            elif not parameters and context.no_parameters:
                if self.dialect._has_events:
                    for fn in self.dialect.dispatch.do_execute_no_params:
                        if fn(cursor, statement, context):
                            evt_handled = True
                            break
                if not evt_handled:
                    self.dialect.do_execute_no_params(
                        cursor, statement, context
                    )
            else:
                if self.dialect._has_events:
                    for fn in self.dialect.dispatch.do_execute:
                        if fn(cursor, statement, parameters, context):
                            evt_handled = True
                            break
                if not evt_handled:
>                   self.dialect.do_execute(
                        cursor, statement, parameters, context
                    )

../../venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py:1248: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <sqlalchemy.dialects.postgresql.psycopg2.PGDialect_psycopg2 object at 0x13f674ca0>
cursor = <cursor object at 0x13eddbe50; closed: -1>
statement = "CREATE FUNCTION\n                queries_search_vector_update() RETURNS TRIGGER AS $$\n            BEGIN\n           ...')), %(setweight_4)s);\n                RETURN NEW;\n            END\n            $$ LANGUAGE 'plpgsql';\n            "
parameters = {'setweight_1': 'B', 'setweight_2': 'A', 'setweight_3': 'C', 'setweight_4': 'D', ...}
context = <sqlalchemy.dialects.postgresql.psycopg2.PGExecutionContext_psycopg2 object at 0x13f734fd0>

    def do_execute(self, cursor, statement, parameters, context=None):
>       cursor.execute(statement, parameters)
E       psycopg2.errors.UndefinedFile: could not access file "$libdir/plpgsql": No such file or directory

../../venv/lib/python3.9/site-packages/sqlalchemy/engine/default.py:580: UndefinedFile

The above exception was the direct cause of the following exception:
../__init__.py:65: in setUp
    db.create_all()
../../venv/lib/python3.9/site-packages/flask_sqlalchemy/__init__.py:1039: in create_all
    self._execute_for_all_tables(app, bind, 'create_all')
../../venv/lib/python3.9/site-packages/flask_sqlalchemy/__init__.py:1031: in _execute_for_all_tables
    op(bind=self.get_engine(app, bind), **extra)
../../venv/lib/python3.9/site-packages/sqlalchemy/sql/schema.py:4303: in create_all
    bind._run_visitor(
../../venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py:2046: in _run_visitor
    conn._run_visitor(visitorcallable, element, **kwargs)
../../venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py:1615: in _run_visitor
    visitorcallable(self.dialect, self, **kwargs).traverse_single(element)
../../venv/lib/python3.9/site-packages/sqlalchemy/sql/visitors.py:138: in traverse_single
    return meth(obj, **kw)
../../venv/lib/python3.9/site-packages/sqlalchemy/sql/ddl.py:777: in visit_metadata
    self.traverse_single(
../../venv/lib/python3.9/site-packages/sqlalchemy/sql/visitors.py:138: in traverse_single
    return meth(obj, **kw)
../../venv/lib/python3.9/site-packages/sqlalchemy/sql/ddl.py:843: in visit_table
    table.dispatch.after_create(
../../venv/lib/python3.9/site-packages/sqlalchemy/event/attr.py:322: in __call__
    fn(*args, **kw)
../../venv/lib/python3.9/site-packages/sqlalchemy_searchable/__init__.py:294: in after_create
    connection.execute(str(clause), **clause.params)
../../venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py:982: in execute
    return self._execute_text(object_, multiparams, params)
../../venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py:1149: in _execute_text
    ret = self._execute_context(
../../venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py:1252: in _execute_context
    self._handle_dbapi_exception(
../../venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py:1473: in _handle_dbapi_exception
    util.raise_from_cause(sqlalchemy_exception, exc_info)
../../venv/lib/python3.9/site-packages/sqlalchemy/util/compat.py:398: in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
../../venv/lib/python3.9/site-packages/sqlalchemy/util/compat.py:152: in reraise
    raise value.with_traceback(tb)
../../venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py:1248: in _execute_context
    self.dialect.do_execute(
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <sqlalchemy.dialects.postgresql.psycopg2.PGDialect_psycopg2 object at 0x13f674ca0>
cursor = <cursor object at 0x13eddbe50; closed: -1>
statement = "CREATE FUNCTION\n                queries_search_vector_update() RETURNS TRIGGER AS $$\n            BEGIN\n           ...')), %(setweight_4)s);\n                RETURN NEW;\n            END\n            $$ LANGUAGE 'plpgsql';\n            "
parameters = {'setweight_1': 'B', 'setweight_2': 'A', 'setweight_3': 'C', 'setweight_4': 'D', ...}
context = <sqlalchemy.dialects.postgresql.psycopg2.PGExecutionContext_psycopg2 object at 0x13f734fd0>

    def do_execute(self, cursor, statement, parameters, context=None):
>       cursor.execute(statement, parameters)
E       sqlalchemy.exc.OperationalError: (psycopg2.errors.UndefinedFile) could not access file "$libdir/plpgsql": No such file or directory
E       
E       [SQL: CREATE FUNCTION
E                       queries_search_vector_update() RETURNS TRIGGER AS $$
E                   BEGIN
E                       NEW.search_vector = ((setweight(to_tsvector(%(to_tsvector_1)s, regexp_replace(coalesce(CAST(NEW.id AS TEXT), ''), '[-@.]', ' ', 'g')), %(setweight_1)s) || setweight(to_tsvector(%(to_tsvector_2)s, regexp_replace(coalesce(NEW.name, ''), '[-@.]', ' ', 'g')), %(setweight_2)s)) || setweight(to_tsvector(%(to_tsvector_3)s, regexp_replace(coalesce(NEW.description, ''), '[-@.]', ' ', 'g')), %(setweight_3)s)) || setweight(to_tsvector(%(to_tsvector_4)s, regexp_replace(coalesce(NEW.query, ''), '[-@.]', ' ', 'g')), %(setweight_4)s);
E                       RETURN NEW;
E                   END
E                   $$ LANGUAGE 'plpgsql';
E                   ]
E       [parameters: {'to_tsvector_1': 'pg_catalog.simple', 'setweight_1': 'B', 'to_tsvector_2': 'pg_catalog.simple', 'setweight_2': 'A', 'to_tsvector_3': 'pg_catalog.simple', 'setweight_3': 'C', 'to_tsvector_4': 'pg_catalog.simple', 'setweight_4': 'D'}]
E       (Background on this error at: http://sqlalche.me/e/e3q8)

../../venv/lib/python3.9/site-packages/sqlalchemy/engine/default.py:580: OperationalError

Process finished with exit code 1

It can work if I run the test using docker command “docker-compose run --rm server tests”. 

I find the reason might be related to multi postgres instances installed on my Mac. But it still does not work after uninstalling all the postgres instances I installed. 

I have tried upgrading my Pycharm version to 2023.2.3. But it still does not work. 

0
1 comment
Hi, is it possible to provide a minimal sample project to reproduce the issue?
0

Please sign in to leave a comment.