Python Forum

Full Version: spread same class into separate files in python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have some automated test written in python. There is file named TestSteps.py that holds all the test methods. The file is getting too big holding too many methods. It looks like this

#TestSteps.py

import myModules

class Steps:
    @staticmethod
    def test1(self):
        ...
    @staticmethod
    def test2(self):
        ...
    @staticmethod
    def testX(self):
The file has now too many methods and I need to add more so I was thinking to split them into multiple files, for example TestSteps1, TestSteps2 etc. and inside TestSteps to import them like this:

#TestSteps.py    

import TestSteps1
reload(TestSteps1)
from TestSteps1 import *
...
import TestStepsX
reload(TestStepsX)
from TestStepsX import *
I need to have all these test files 'stored' under TestSteps.py because of how everything is configured, how the test are executed etc. With the approach I've tried above only the tests from first file are executed, the rest are not found.

So how can I spread for example 'class Steps' into multiple files? Or how can I import all my test method from different files into a central files that would be 'TestSteps.py'?
Maybe you need to spread the code into multiple classes as well. Its hard to tell without seeing the code.

(Jun-19-2019, 01:16 PM)asheru93 Wrote: [ -> ]Or how can I import all my test method from different files into a central files that would be 'TestSteps.py'?
You should only have to put anything else in another file and import that file into TestSteps.py if that is your root file to transfer the load to another module.
I solved it like this with a basic class extend

from TestSteps1 import Tests

class Steps(Tests):
    ....
However i had to also the way tests are executed because of how it was build it the past...in a pretty customized and dynamic way. Anyway this above had the answer to my question.
I would agree with spreading it into multiple classes. When I'm building test code, I have one file of test code for each file in the program (and you may want to break your program up into more files). Each file has multiple test classes, each class typically for a different method or function. Each method of a test class tests a specific situation.

You might want to look at some of the test packages available. I use unittest, and it makes it easy to make the test classes and to share code between test cases. I can easily run all of the test cases in a file with one line, and easily make a program that runs all of the test files in a folder.