pyats icon indicating copy to clipboard operation
pyats copied to clipboard

running package.module directly under aetest & easypy

Open extsoft opened this issue 6 years ago • 6 comments

I have the following setup:

# files
$ tree fooo/
fooo/
├── __init__.py
├── alisa.py
└── foo.py
$ python fooo/alisa.py 
Traceback (most recent call last):
  File "fooo/alisa.py", line 5, in <module>
    from fooo.foo import fooo
ModuleNotFoundError: No module named 'fooo'

$ cat -b  fooo/__init__.py fooo/alisa.py fooo/foo.py 
     1	from ats import aetest

     2	from ats.aetest import test

     3	from fooo.foo import fooo


     4	class Smoke(aetest.Testcase):
     5	    @test
     6	    def test_one(self):
     7	        fooo()
     8	        print('Alisa is fine!')


     9	class Health(aetest.Testcase):
    10	    @test
    11	    def status(self):
    12	        print('Alisa\'s health is fine!')


    13	if __name__ == '__main__':
    14	    aetest.main()
     1	def fooo():
     2	    print('fooo')

Is there a chance to run a test without modifying a sys.path? As alisa.py is within a package, looks like it has to understand the context based packages logic.

extsoft avatar Sep 13 '18 12:09 extsoft

hmmmm, that's a good observation.

we do add to sys.path the location of the script file - primarily because the script itself often is in an arbitrary location (eg, it doesn't have to be part of your PYTHNOPATH).

removing that would interfere with this functionality.

however, with that being said, the loader does support a module directly -

if you write a top level entry.py file with


from fooo import alisa

from ats.aetest import main

main(alisa)

should work

simingy avatar Sep 13 '18 13:09 simingy

@siming85 the same behavior is in the job file. Is it possible to support a sys.path of the location of the script and/or job file in the future release?

extsoft avatar Sep 13 '18 13:09 extsoft

think so, will put it into todos.

simingy avatar Sep 13 '18 13:09 simingy

a couple note on this...

aetest already supports loading by module paths.

so inside a job file:

def main():

    run('mypackage.myscriptmodule')

does work.

in your above fooo example, it would equate to

def main():
    run('fooo.alisa')

and the sys.path doesn't change in this case i think it should be documented, but probably just a blur atm

job doesn't support loading by module, that's something i can look into adding.

$ python -m pyats.aetest fooo.alisa
2018-09-26T13:52:47: %AETEST-INFO: Starting testcase Smoke
2018-09-26T13:52:47: %AETEST-INFO: Starting section test_one
fooo
Alisa is fine!
2018-09-26T13:52:47: %AETEST-INFO: The result of section test_one is => PASSED
2018-09-26T13:52:47: %AETEST-INFO: The result of testcase Smoke is => PASSED
2018-09-26T13:52:47: %AETEST-INFO: Starting testcase Health
2018-09-26T13:52:47: %AETEST-INFO: Starting section status
Alisa's health is fine!
2018-09-26T13:52:47: %AETEST-INFO: The result of section status is => PASSED
2018-09-26T13:52:47: %AETEST-INFO: The result of testcase Health is => PASSED
2018-09-26T13:52:47: %AETEST-INFO: +------------------------------------------------------------------------------+
2018-09-26T13:52:47: %AETEST-INFO: |                               Detailed Results                               |
2018-09-26T13:52:47: %AETEST-INFO: +------------------------------------------------------------------------------+
2018-09-26T13:52:47: %AETEST-INFO:  SECTIONS/TESTCASES                                                      RESULT
2018-09-26T13:52:47: %AETEST-INFO: --------------------------------------------------------------------------------
2018-09-26T13:52:47: %AETEST-INFO: .
2018-09-26T13:52:47: %AETEST-INFO: |-- Smoke                                                                PASSED
2018-09-26T13:52:47: %AETEST-INFO: |   `-- test_one                                                         PASSED
2018-09-26T13:52:47: %AETEST-INFO: `-- Health                                                               PASSED
2018-09-26T13:52:47: %AETEST-INFO:     `-- status                                                           PASSED
2018-09-26T13:52:47: %AETEST-INFO: +------------------------------------------------------------------------------+
2018-09-26T13:52:47: %AETEST-INFO: |                                   Summary                                    |
2018-09-26T13:52:47: %AETEST-INFO: +------------------------------------------------------------------------------+
2018-09-26T13:52:47: %AETEST-INFO:  Number of ABORTED                                                            0
2018-09-26T13:52:47: %AETEST-INFO:  Number of BLOCKED                                                            0
2018-09-26T13:52:47: %AETEST-INFO:  Number of ERRORED                                                            0
2018-09-26T13:52:47: %AETEST-INFO:  Number of FAILED                                                             0
2018-09-26T13:52:47: %AETEST-INFO:  Number of PASSED                                                             2
2018-09-26T13:52:47: %AETEST-INFO:  Number of PASSX                                                              0
2018-09-26T13:52:47: %AETEST-INFO:  Number of SKIPPED                                                            0
2018-09-26T13:52:47: %AETEST-INFO: --------------------------------------------------------------------------------
passed

simingy avatar Sep 26 '18 17:09 simingy

@siming85 also solution from this comment works well. It will be great to have the same for easypy.

extsoft avatar Sep 26 '18 19:09 extsoft

i've been looking into adding this to easypy. I think it would be a great addition, but because easypy was originally derived to work with job file, adding support for modules theoretically isn't hard (and it isn't), but there are some code dependencies here and there that has to be move around.

going to post pone to next release instead.

simingy avatar Sep 27 '18 18:09 simingy