Good day. I am engaged in testing, I use Selenium and Python.

It became necessary to record all the tests in 1 file and run the script with the parameters and so that the selected tests were performed.

I feel that there is a lack of fundamental knowledge, but googling on specifics, did not lead to an answer, nor to the direction of where to look.

Now my tests are broken down into different files, and each one has hardcodes where to enter, what kind of login password and so on.

class loginTest(unittest.TastCase): def setUp(self): #открываю браузер и нужную страницу def test_1(self): # логинюсь и выполняю набор кейсов def test_2(self): # логинюсь и выполняю набор кейсов def test_3(self): # логинюсь и выполняю набор кейсов def test_4(self): # логинюсь и выполняю набор кейсов def tearnDown(self): #Забираю из памяти теста лог, организую отчёт if __name__ == '__main__': unittest.main() 

What I want to do looks mock-up like this, but how can I choose when I run the script, run test 1 and 4 or any other set, there are no ideas. Who faced such, prompt, I will be glad even to the link where it is possible to read.

  • one
    Tried to use parameters from a config? Set parameters on the command line? well, or ask to enter parameters in the application itself .. - Anton Komyshan
  • The fact is that when an inttest class is declared, selenium takes and performs all tests in a row, without parsing. And I don’t know the mechanism of how, where and what parameter to pass in order to run a particular test separately. - Nick Su
  • Well, if I understood correctly, then from the command line this is done like this: python -m unittest test_module.TestClass.test_method . This is what you need? - Anton Komyshan
  • Almost, Now I run just like this (python testv1.py), but I want something like this (python test.py -m 145), where the numbers are test numbers (it can be written entirely, so as not to suffer from the association and test) - Nick Su

2 answers 2

As a result, I found a way to run the selected tests. We do this through the Suite, and it is not difficult to paste the parameter to the function.

 # -*- coding: utf-8 -*- from selenium import webdriver import unittest class testsall (unittest.TestCase): def test_open_page(self): wd = webdriver.Firefox() wd.maximize_window() wd.implicitly_wait(60) wd.get("http://ukr.net") wd.close() def suite(): suite1 = unittest.TestSuite() suite1.addTest(testsall("test_open_page")) return suite1 if __name__ == "__main__": with open('/home/andrey/test.log','w') as logf: unittest.TextTestRunner(verbosity=2, stream = logf).run(suite()) 

    put any character before the test. The method will be ignored. Or commented out. Example: def _test_1 (self):

    Although maybe I'm wrong. Only 3 days how to teach Python) (correct me if the nonsense is generated :))

    • Answer questions you are sure of. - 0xdb