Is there a True constant with unittesting, but False otherwise?

In the Python module typing, they have a really useful constant, which is Truetype checking, but Falseotherwise. This means, for example, that you can dynamically import classes if TYPE_CHECKINGevaluated as True.

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from module import Class

It would be very helpful if there unittestwas something like that. I can see in the file __init__.py, there is a variable defined as __unittest = True:

__all__ = ['TestResult', 'TestCase', 'TestSuite',
           'TextTestRunner', 'TestLoader', 'FunctionTestCase', 'main',
           'defaultTestLoader', 'SkipTest', 'skip', 'skipIf', 'skipUnless',
           'expectedFailure', 'TextTestResult', 'installHandler',

__unittest = True

Can I use the __unittestsame as TYPE_CHECKINGfrom typing?

: , . , , . , .

!

+4
3

: , . , , . , .

- . "" . " " , "" .. , / .

, unittest. , , unittests - .

, , matplotlib - . QA.

+3

, (, unittest ), , "unittest" sys.modules

import sys
import unittest

if "unittest" in sys.modules:
    print("I'm unittest-ing")
+1

.

, is_unit_testing , -unittest python.

, : Python?

import sys
# sys.argv is a list of all commandline arguments you can check through
command_line_arguments_list = sys.argv # e.g. ["arg1", "arg2", "-unittest"]
is_unit_testing = "-unittest" in command_line_arguments_list

You can then pass the arguments, as you expect, through the command line:

python myModule.py  arg1  arg2  -unittest

This works well for anything if you want multiple “builds” to be with the same code base. For example, the presence of the "no database" / "no gui" mode, etc.

0
source

Source: https://habr.com/ru/post/1685467/


All Articles