2

I have a set of python/selenium/unittest tests contained in a single class:

class TestProject(unittest.TestClass):
    def test001_create_project(self):
        ...
    def test002_project_wizard_page1(self, projectName, projectDescription):
        ....
    def test003_project_wizard_page2(self):
        ....
    def test004_project_wizard_finish(self):
        ....

I need to run the test methods in a block in the above order because they walk through a wizard on my web application. However, I want to be able to pass a variety of parameters to the test methods like projectName, projectDescription, etc. Using unittest, how can I run a set of maybe 10 iterations of the tests passing in different parameters to test002_project_wizard_page1?

dnraikes
  • 275
  • 1
  • 4
  • 14
  • Does this answer your question? [How do you generate dynamic (parameterized) unit tests in Python?](https://stackoverflow.com/questions/32899/how-do-you-generate-dynamic-parameterized-unit-tests-in-python) – outis Jun 22 '21 at 22:32

4 Answers4

6

Your numbered tests are really just parts of a single test. What you should define as separate tests are functions that use your parameter sets.

class TestProject(unittest.TestCase):
    def _create_project(self):
        ...
    def _page1(self, projectName, projectDescription):
       ...
    def _page2(self):
       ...
    def _finish(self):
       ...

    def _run(self, name, descr):
        self._create_project()
        self._page1(name, descr)
        self._page2()
        self._finish()

    def test001(self):
        self._run("foo", "do foo")

    def test002(self):
        self._run("bar", "do bar")

    # etc

An interesting module that can reduce some of the boilerplate is the ddt project.

import ddt

@ddt.ddt
class TestProject(unittest.TestCase):
    def _create_project(self):
        ...
    def _page1(self, projectName, projectDescription):
       ...
    def _page2(self):
       ...
    def _finish(self):
       ...

    @ddt.data(
      ("foo", "do foo"),
      ("bar", "do bar"),
      # etc
    )
    @ddt.unpack
    def test_run(self, name, descr):
        self._create_project()
        self._page1(name, descr)
        self._page2()
        self._finish()
chepner
  • 497,756
  • 71
  • 530
  • 681
1

Any reason that you cannot use pytest? It provides this functionality out of the box.

MrName
  • 2,363
  • 17
  • 31
  • this is a good solution, but I like the idea of using a json file as provided by the accepted anser and ddt to extend the number and variety of test parameters. The json file can be provided as an input to my jenkins job that runs the tests. – dnraikes May 24 '18 at 18:26
0

For me, it looks like you wanna test under a template, with different parameters. How about codes like the following:

import unittest


class TestProject(unittest.TestClass):
    def mytest001_create_project(self):
        ...

    def mytest002_project_wizard_page1(self, projectName, projectDescription):
        ....

    def mytest003_project_wizard_page2(self):
        ....

    def mytest004_project_wizard_finish(self):
        ....

    def mytest_in_order(self, project_name, project_description):
        self.mytest001_create_project()
        self.mytest002_project_wizard_page1(project_name, project_description)
        self.mytest003_project_wizard_page2()
        self.mytest004_project_wizard_finish()

    def test_in_batch(self):
        project_names = ['a', 'b']
        project_descriptions = ['aa', 'bb']

        for project_name, project_description in zip(project_names, project_descriptions):
            self.mytest_in_order(project_name, project_description)
Menglong Li
  • 2,177
  • 14
  • 19
0

Take a look at Python unit testing: parametrized test cases post on Eli Bendersky's blog. It states that

You can't easily pass arguments into a unittest.TestCase from outside.

However, it provides an implementation for class ParametrizedTestCase(unittest.TestCase) which can be used to add parameters to the unittest.TestCase class. That will solve your parameters problem and I believe, each individual test methods are already being run in order.

Faboor
  • 1,365
  • 2
  • 10
  • 23