Search code examples
bazel

Using binary in python test in bazel


I would like to write some end-to-end tests of my C++ binary, that would provide some input file and read output from stdout and assert it's correct. I wrote a simple python test and it works fine and now I try to make it work with bazel. I added py_test to bazel and it builds, but I can't specify cc_binary in deps of that target (bazel complains). If I don't run the build command of the cc_binary separately, the python test won't see the binary in bazel-bin. How can I force to also build my cc_binary before running py_test?


Solution

  • You can add the binary as a data dependency. See the encyclopedia for details, but it would basically look something like:

    cc_binary(
        name = "my-bin",
        srcs = ["bin.cc"],
    )
    
    py_test(
        name = "my-test",
        srcs = ["my_test.py"],
        data = [":my-bin"],
        # any other attributes you need...
    )