1

I'm working on creating tests for the first time using the unittest package in Python. The part of the project I'm writing tests for now is a creation of a new database file (SQLite) and ensuring the various tables were set up properly. I have three tables that will have the same structure, so the tests are identical except for the table names.

Because it's so similar, I was trying to think in terms of the DRY method, and wanting to make one test function that could be used across the three tables with a parameter providing the table name of the table I want to test, then just calling the test three times, once with each table name. Something like the following:

 def NameExists(self, table_name):
     self.assertEqual('Name', self.db.models[table_name].record(0).value('Field Name'))

What I'm struggling with is that it appears unittest doesn't allow for parameters. Nothing I'm finding shows that parameters are supported for unittest in any way, shape, or form. I'm trying to figure out, aside from copy/pasting repetitive code, whether or not there is an intended/common way to create reusable test code with unittest.

Any thoughts/ideas would be greatly appreciated. Thanks in advance!!

Jason Harrer
  • 107
  • 5
  • you can do a for-loop – drum Mar 18 '20 at 03:48
  • If I could send parameters, I would agree with you. I'm stuck with a definition of def testName(self) Without being able to add parameters, the for loop is useless. – Jason Harrer Mar 18 '20 at 03:51
  • the incoming arg here is `table_name`, using `self` as a reference to another def function, if you are getting some sort of positional error or keyword argument error...as we don't know what your **traceback** error is like. the pass by reference for `table_name` is what's being used here – de_classified Mar 18 '20 at 04:02
  • If you are not set to use `unittest`, you could switch to `pytest`, which supports parameters and is replacing `unittest` in usage more and more. – MrBean Bremen Mar 18 '20 at 07:02
  • @FishingCode Yes, in my example, I did have a parameter. That was an example of what I wished I could do, not what Unittest allows me to do. Unittest only allows for (self), due to the way the package works and how you add Test Cases and how they're called. Adding test cases doesn't allow you to add parameters at that point. If it were as simple as me writing a function with additional parameters, I would totally have done that. – Jason Harrer Mar 18 '20 at 13:20
  • Thanks for the recommendation, @MrBeanBremen. I'll check out pytest. – Jason Harrer Mar 18 '20 at 13:20
  • @Jason Harrer, I think this question similar to what you're trying to get done here. See the answer with 6 upvotes, you may have to play around with what you have, possibly add a class which will represent your incoming arg parameter. Here's the [link](https://stackoverflow.com/questions/17260469/instantiate-python-unittest-testcase-with-arguments). Hope it helps. – de_classified Mar 18 '20 at 15:15

0 Answers0