Home > front end >  bazel run :testing_example returns ModuleNotFoundError: No module named 'absl.testing'
bazel run :testing_example returns ModuleNotFoundError: No module named 'absl.testing'

Time:06-28

I'm studying Abseil and Bazel, doing some code regarding Unit Tests Basics I got a problem regarding the absltest module:

testing_example.py:

from absl import app
from absl import flags
from absl.testing import absltest


FLAGS = flags.FLAGS
class TestStringMethods(absltest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    absltest.main()

BUILD:

py_binary(
  name="testing_example",
  deps = ["@io_abseil_py//absl:app"],
  srcs = ["testing_example.py"],
)
$ python testing_example.py 
Running tests under Python 3.10.5: /Users/maion/opt/anaconda3/envs/abslPy/bin/python
[ RUN      ] TestStringMethods.test_isupper
[       OK ] TestStringMethods.test_isupper
[ RUN      ] TestStringMethods.test_split
[       OK ] TestStringMethods.test_split
[ RUN      ] TestStringMethods.test_upper
[       OK ] TestStringMethods.test_upper
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK

But when I run with bazel I get the following error:

$ bazel run //example_py:testing_example
(...)
INFO: Analyzed target //example_py:testing_example (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //example_py:testing_example up-to-date:
  bazel-bin/example_py/testing_example
INFO: Elapsed time: 0.108s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
Traceback (most recent call last):
  File "/private/var/tmp/_bazel_maion/29bdcd3c09c56f1b80d3a22707ccfd7e/execroot/__main__/bazel-out/darwin_arm64-fastbuild/bin/example_py/testing_example.runfiles/__main__/example_py/testing_example.py", line 3, in <module>
    from absl.testing import absltest
ModuleNotFoundError: No module named 'absl.testing'

Why it is not finding the module absl.testing?

CodePudding user response:

Ok, I got it:

I was having trouble finding the path to the library I needed to import (@io_abseil_py//absl/testing:absltest). Here is the updated BUILD

py_binary(
  testonly = 1,
  name="testing_example",
  deps = ["@io_abseil_py//absl:app", "@io_abseil_py//absl/testing:absltest"],
  srcs = ["testing_example.py"],
) 

Solved. Output:

$ bazel run //example_py:testing_example
(...)
INFO: Analyzed target //example_py:testing_example (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //example_py:testing_example up-to-date:
  bazel-bin/example_py/testing_example
INFO: Elapsed time: 0.236s, Critical Path: 0.01s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
Running tests under Python 3.9.7: /Users/maion/opt/anaconda3/bin/python3
[ RUN      ] TestStringMethods.test_isupper
[       OK ] TestStringMethods.test_isupper
[ RUN      ] TestStringMethods.test_split
[       OK ] TestStringMethods.test_split
[ RUN      ] TestStringMethods.test_upper
[       OK ] TestStringMethods.test_upper
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK
  • Related