Python Forum
spread same class into separate files in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
spread same class into separate files in python
#1
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'?
Reply
#2
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.
Recommended Tutorials:
Reply
#3
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.
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Class test : good way to split methods into several files paul18fr 4 405 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  Split Bytearray into separate Files by Hex delimter lastyle 5 2,493 Mar-09-2023, 07:49 AM
Last Post: bowlofred
  Python Split json into separate json based on node value CzarR 1 5,476 Jul-08-2022, 07:55 PM
Last Post: Larz60+
  importing functions from a separate python file in a separate directory Scordomaniac 3 1,331 May-17-2022, 07:49 AM
Last Post: Pedroski55
  How to save files in a separate directory Scordomaniac 3 1,755 Mar-16-2022, 10:17 AM
Last Post: Gribouillis
  Separate text files and convert into csv marfer 6 2,798 Dec-10-2021, 12:09 PM
Last Post: marfer
  Pythonic way to handle/spread alerts class in multiple modules psolar 11 4,487 Feb-12-2020, 04:11 PM
Last Post: psolar
  write each line of a text file into separate text files and save with different names Shameendra 3 2,745 Feb-20-2019, 09:07 AM
Last Post: buran
  instance of a class in different files DionisiO 2 2,414 Jan-21-2019, 09:38 PM
Last Post: DionisiO
  Running a python tool transforming xml files into epub files silfer 7 5,315 May-10-2018, 03:49 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020