-1

I currently have some tests that are structured like this:

tests/
    category_a/
        sub_category_1/
            test_file_a11.py
            test_file_a12.py
        sub_category_2/
            test_file_a21.py
            test_file_a22.py
        sub_category_3/
            test_file_a31.py
            test_file_a32.py
    category_b/
        sub_category_4/
            test_file_b41.py
            test_file_b42.py
        sub_category_5/
            test_file_b51.py
            test_file_b52.py
        sub_category_6/
            test_file_b61.py
            test_file_b62.py

Which works really well for what we need. In reality this breakdown makes more sense, and there are many more categories/files. This can also apply just to our non-test structure.

The problem is that I want to have multiple levels of shared code. Some code that should be shared between ALL tests, some just between tests in category_a, and some specific to sub categories. I can just add a common.py at each level, but then I end up with test_file_a32 using 3 different common files. It works, but I find it pretty confusing to read.

Is there a better way to do this?

user3715648
  • 1,498
  • 3
  • 16
  • 25
  • 1
    Use classes and subclasses. The subclasses should be based on the `common.py` tests and will inherit all of the methods/functions available. – Adi219 Jun 08 '18 at 18:53
  • @Adi219 What would you name each file then? – user3715648 Jun 08 '18 at 18:57
  • 1
    `all_tests.py` just inside the `/tests` directory, `a_tests.py` just inside the `/tests/a` directory, and `subcat1_tests.py` inside the `/tests` directory. Alternatively, you could have one file containing all of these common tests just inside the `/tests` directory and just access the different tests from within the different files. – Adi219 Jun 08 '18 at 19:00

1 Answers1

1

In industry, it's common for test teams to implement what you're describing using classes and subclasses. The parent class should be called common.pyand its subclasses should contain their own specialised tests (the common.py tests would be inherited).

Adi219
  • 4,712
  • 2
  • 20
  • 43
  • Makes sense. What would the naming structure be though? Top level is common.py, next level is category_a_common.py, then sub_category_common.py? Or is there a different convention that's commonly used? – user3715648 Jun 08 '18 at 19:01
  • @user3715648 See my comment on your question :) – Adi219 Jun 08 '18 at 19:01
  • @user3715648 https://stackoverflow.com/questions/96297/what-are-some-popular-naming-conventions-for-unit-tests – Adi219 Jun 08 '18 at 19:04
  • @user3715648 Using underscores and the type of naming you're using is a common practice :) – Adi219 Jun 08 '18 at 19:04