tl;dr: Is there a way to fully stop and restart PyMOL "sessions" using the API 
to avoid crossover between unit tests?

Hi everyone -

I'm working on unit testing for a plugin and have been trying to figure out how 
to test some functionality from within a PyMOL session.  I have a partially 
working solution, but the problem arises that I can't adequately reinitialize 
PyMOL between tests, and sometimes end up getting carryover between tests.

Here's a working example of this behavior using the built-in `unittest` testing 
framework:

```
import unittest

import __main__
__main__.pymol_argv = ['pymol','-qkc']
import pymol
from pymol import cmd

class TestPymolApi(unittest.TestCase):
    def setUp(self):
        pymol.finish_launching()
        cmd.reinitialize()

    def tearDown(self):
        #cmd.quit()  # ***this line hangs if uncommented***
        pass

    def test_1(self):
        '''No value for stored.foo.'''
        from pymol import stored
        with self.assertRaises(AttributeError):
            stored.foo

    def test_2(self):
        '''Sets a value for stored.foo.'''
        from pymol import stored
        stored.foo = 'bar'

    #def test_3(self):
    #    '''Fails due to carryover from test_2.'''
    #    self.test_1()

if __name__ == '__main__':
    unittest.main()
```


Running `python basic_tests.py` yields the following output (each dot 
represents a successful test):

```
$ python basic_tests.py
..
----------------------------------------------------------------------
Ran 2 tests in 0.043s

OK
```


But if I uncomment `test_3` and re-run (the tests are executed in standard 
string-sorting order by function name), it fails because the value of 
`stored.foo` is retained even after reinitialization after `test_2`.  (The 
`setUp` and `tearDown` functions are run before and after each test, 
respectively.)

```
$ python basic_tests.py
..F
======================================================================
FAIL: test_3 (__main__.TestPymolApi)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "basic_tests.py", line 32, in test_3
    self.test_1()
  File "basic_tests.py", line 24, in test_1
    stored.foo
AssertionError: AttributeError not raised

----------------------------------------------------------------------
Ran 3 tests in 0.110s

FAILED (failures=1)
```

Maybe this is a bug in `cmd.reinitialize()`?  If not, it is unexpected behavior 
(at least in my opinion), and could be improved.

Anyway, this is why I'm trying to figure out another way to reset PyMOL using 
the API.  So if I then uncomment the `cmd.quit()` line to try to get complete 
shut down and restart of PyMOL, unittest hangs on the tearDown of the first 
test and I have to interrupt the process with <Ctrl-C>.

I found an old 
thread<https://sourceforge.net/p/pymol/mailman/message/26469705/> where Jason 
mentioned using (admittedly experimental) `pymol2.PyMOL().start/stop()` to, 
well, start and stop PyMOL sessions.  However, in Open Source PyMOL 1.8.2.1, it 
appears to be broken:

```
$ python
>>> import pymol2
>>> p = pymol2.PyMOL()
>>> p.start()
Segmentation fault: 11
```

So, again, my question is: Is there any way to reliably start and stop multiple 
PyMOL sessions or completely reset it using the API?

Looking forward any suggestions or further discussion.

Cheers,
Jared








------------------------------------------------------------------------------
Attend Shape: An AT&T Tech Expo July 15-16. Meet us at AT&T Park in San
Francisco, CA to explore cutting-edge tech and listen to tech luminaries
present their vision of the future. This family event has something for
everyone, including kids. Get more information and register today.
http://sdm.link/attshape
_______________________________________________
PyMOL-users mailing list (PyMOL-users@lists.sourceforge.net)
Info Page: https://lists.sourceforge.net/lists/listinfo/pymol-users
Archives: http://www.mail-archive.com/pymol-users@lists.sourceforge.net

Reply via email to