how to reuse tests written using unittest.testcase

2024/11/13 15:02:42

I've written some tests using unittest as below and I want to reuse them in another class where I'm stuck and need help.. Code snippets are as below.

    MyTestClass.pyClass MyTestClass(unittest.TestCase):   @classmethoddef test_TC01_set(self):self.devAddr = "127.0.0.0"self.teststoSkip = 'TC02'def skip(type):if type in self.teststoSkip:self.skipTest('skipped!!') #unittest.Testcase methoddef test_TC02(self):self.skip('TC02')print 'test_TC02 will do other tasks'def test_TC03(self):self.skip('TC03')print 'test_TC03 will do other tasks'

This will work fine. Now I want to reuse the same testcases in another class. say,

    RegressionMyTest.pyfrom MyTestClass import MyTestClassClass RegressionMyTest(MyTestClass):@classmethoddef setupmytest(self):self.test_TC01_set(self)#this will work fine since it is accessing classmethodself.tes_TC02(self)#cant access like this since it is not a class methodself.tes_TC03(self)#cant access like this since it is not a class method

How can I reuse the tests in MyTestClass in RegressionMyTest so that both MyTestClass and RegressionMyTest should work if they are run individually using nosetests/unittest.

Answer

Usually tests are supposed to assert code is functioning in a certain way, so I'm not sure if it would make sense to actually share tests between testsuites, (I don't think it would be very explicit)

Python tests cases are just python classes, that are introspected by the test runner for methods beginning in test_. Because of this you can you use inheritance in the same way you would with normal classes.

If you need shared functionality, you could create a base class with shared initialization methods/ helper methods. Or create testing mixins with utility functions that are needed across tests.

    class BaseTestCase(unittest.TestCase):def setUp(self):# ran by all subclassesdef helper(self):# helpclass TestCaseOne(BaseTestCase):def setUp(self):# additional setupsuper(TestCaseOne, self).setUp()def test_something(self):self.helper() # <- from base

Perhaps you don't want to define a setup method on the base class and just define on child classes using some helper methods defined in the base class? Lots of options!

https://en.xdnf.cn/q/72070.html

Related Q&A

Randomized stratified k-fold cross-validation in scikit-learn?

Is there any built-in way to get scikit-learn to perform shuffled stratified k-fold cross-validation? This is one of the most common CV methods, and I am surprised I couldnt find a built-in method to …

Find all paths through a tree (nested dicts) from top to bottom

EDIT: See below for a suggested answer and how its not quite right yet.There are many similar questions to this one on Stack Overflow, but none exactly like it in Python. Im a programming novice, so pl…

How to show process state (blocking, non-blocking) in Linux

Is there a way to query the state of processes in a Linux process table to be able to demonstrate if a process is running or blocked at the time the query is executed? My goal is to do this from outsi…

Removing quotation marks from list items

I am running this program:f = open( "animals.txt", "r") g = f.read() g1 = g.split(,) #turning the file into list print g1And I want this to come out:[ELDEN, DORSEY, DARELL, BRODERIC…

Handle multiple questions for Telegram bot in python

Im programming a telegram bot in Python using the Telegram bot API. Im facing the problem of managing questions that need an answer of the user. The problem arises when the program is waiting for an an…

Which GTK+ elements support which CSS properties?

While applying my own CSS to my GTK+ application, I noticed, that some elements ignore some CSS properties and others ignore others or dont ignore them, which leads me to search for an overview of whic…

Self import of subpackages or not?

Suppose you have the following b b/__init__.py b/c b/c/__init__.py b/c/d b/c/d/__init__.pyIn some python packages, if you import b, you only get the symbols defined in b. To access b.c, you have to exp…

why is my text not aligning properly in wxPython?

Im using wxPython to build a GUI and Im trying to align some text but its not working at all. Im trying align three different static text items in three places (right aligned, center aligned, and left …

Python subprocess check_output decoding specials characters

Im having some issues with python encoding. When I try to execute this:subprocess.check_output("ipconfig", shell=True)it gives me an output with special characters in it, like:"Statut du…

Python - SystemError: NULL result without error in PyObject call

The story: Im trying to interface from C to Python in order to use the faster computational speed of C for an existing Python code. I already had some success, also with passing NumPy arrays - but now …