0

Is it possible to run Python modules (.py files, NOT methods) listed in a testNG-like python or configuration file(or any other way) to run in a specific/specified order?

I know that each test case should be fully standalone, but I need to run

  1. Signup (which will retrieve DB key for finalizing the signup process) - a separate .py file, needs to run first
  2. Turn on some custom fields - a separate .py file (preferably several .py files if possible so that one file is not too big), needs to run second
  3. Then run all other test cases without any order - a lot of .py files, each file is a test case

This ordering is crucial, otherwise everything fails

This can be done with Java and testNG.xml using preserve-order="true" , but I can't find any solution for Python, especially looked into Proboscis

Any help is appreciated

Arm91
  • 125
  • 1
  • 1
  • 8
  • Depends what test runner you're using exactly. By default [unittest will sort test cases by test name](https://docs.python.org/3/library/unittest.html#unittest.TestLoader.sortTestMethodsUsing). If you're using Python 3.6, you might be able to leverage [PEP 520](https://www.python.org/dev/peps/pep-0520/), override `sortTestMethodUsing` to return `None` (probably, to check) and it'll keep the existing order which *should* be source ordering. Alternatively, just call your test cases `test_00_foo`, `test_01_bar`, ... – Masklinn Mar 09 '20 at 15:02
  • Are you looking for [``TestCase.setUp``](https://docs.python.org/3/library/unittest.html#unittest.TestCase.setUp) perhaps? You mention that you want to run a class, but also say .py files – that's modules, not classes; What exactly do you want to run? What do you consider "to run Python classes" in the first place? A class is instantiated, not run. – MisterMiyagi Mar 09 '20 at 15:44
  • Sorry for confusion, I have edited my initial question. I need to run modules(.py files) in specific order. The application is too big to have numbered files, it won't work that way, it is a big Page Object Model Framework – Arm91 Mar 12 '20 at 13:03

1 Answers1

0

Tests executed through should be designed in such a way that they should be able to be run independently. Pure unit tests offer a benefit that when they fail, they often depicts what exactly went wrong. Incase of functional tests or system tests using unittest framework, it won't be feasible to execute them without ordering them since Selenium automates the Browsing Context. To achieve the ordering, you at-least need to use a better naming convention for the testnames, as an example: test_1, test_2, test_3, etc and this works because the tests are sorted respect to the built-in ordering for strings. An example:

  • Code:

    import unittest
    
    class Test(unittest.TestCase):
    
        def test_1(self):
        print("I'm in test 1")
    
        def test_2(self):
        print("I'm in test 2")
    
        def test_3(self):
        print("I'm in test 3")
    
    if __name__ == "__main__":
        unittest.main()
    
  • Console Output:

    Finding files... done.
    Importing test modules ... done.
    
    I'm in test 1
    I'm in test 2
    I'm in test 3
    ----------------------------------------------------------------------
    Ran 3 tests in 0.001s
    
    OK
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352