简体   繁体   English

如何在没有 AttributeError 的情况下在 GitLab CI 管道中使用变量?

[英]How to use Variables in GitLab CI pipeline without AttributeError?

I am using a variable to define the ROOT_DIR in my .gitlab-ci.yml我正在使用一个变量在我的 .gitlab-ci.yml 中定义 ROOT_DIR

variables:
  ROOT_DIR: "/builds/company/projects/projectname/"

in the job I call the test.py function:在工作中我调用 test.py 函数:

ut-job:
  stage: test
  script:
    - echo "Unit testing ..."
    - python3 tests/test_file.py "$ROOT_DIR"

In the test_file.py I call the command line inout as follows:在 test_file.py 我调用命令行 inout 如下:

if __name__ == "__main__":
    if sys.platform == "Darwin" or sys.platform == "Windows":
        load_dotenv()
        ROOT_DIR = os.getenv("ROOT_DIR")
    else:
        ROOT_DIR=sys.argv[1]    
    print("PLatform: " + sys.platform)
    print("ROOT_DIR: " + ROOT_DIR)
    
    unittest.main()

The printstatement in the pipeline output correctly prints the ROOT_DIR, so the sys.argv gets the variable correctly.管道输出中的 printstatement 正确打印了 ROOT_DIR,因此 sys.argv 正确获取了变量。

However, the pipeline fails with但是,管道失败了

AttributeError: module '__main__' has no attribute '/builds/company/projects/projectname/'

Meaning, the test_file.py main gets the Variable but somehow tries to use it also as an attribute.意思是, test_file.py main 获取变量,但以某种方式尝试将其也用作属性。

Can somebody hint me what I did wrong?有人可以提示我我做错了什么吗?

The issue here is that when you call unittest.main , it inspects the contents of sys.argv for parameters, in this case, test names.这里的问题是,当您调用unittest.main时,它会检查sys.argv的内容以获取参数,在本例中是测试名称。 It will try to use provided arguments to get tests to run by using getattr on the current module.它将尝试使用提供的参数通过在当前模块上使用 getattr 来运行测试。 In this case, resulting in an attribute error.在这种情况下,导致属性错误。

For example, suppose you have a test file ( t.py ) like this:例如,假设您有一个像这样的测试文件 ( t.py ):

import unittest

class Foo(unittest.TestCase):
    def test_foo(self):
        ...

class Bar(unittest.TestCase):
    def test_bar(self):
        ...

unittest.main()

Observe the different results when adding arguments to this file:向此文件添加参数时观察不同的结果:

With no arguments 2 tests run:不带参数运行 2 个测试:

$ python3 ./t.py
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

With the argument Foo only one test ( Foo ) runs:使用参数Foo只有一个测试( Foo )运行:

$ python3 ./t.py Foo
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

If you provide an argument of a nonexistent test (say, Baz ) you'll get an attribute error for the argument passed:如果您提供不存在测试的参数(例如Baz ),您将收到传递参数的属性错误:

$ python3 ./t.py Baz
E
======================================================================
ERROR: Baz (unittest.loader._FailedTest)
----------------------------------------------------------------------
AttributeError: module '__main__' has no attribute 'Baz'

----------------------------------------------------------------------
Ran 1 test in 0.000s

To resolve this you can either (1) not pass any arguments for your script or (2) modify sys.argv before calling unittest.main要解决此问题,您可以 (1) 不为脚本传递任何参数或 (2) 在调用unittest.main之前修改sys.argv

if __name__ == "__main__":
    if sys.platform == "Darwin" or sys.platform == "Windows":
        load_dotenv()
        ROOT_DIR = os.getenv("ROOT_DIR")
    else:
        ROOT_DIR=sys.argv[1]
        sys.argv.pop(1)   # remove rootdir from cli arguments 
    print("PLatform: " + sys.platform)
    print("ROOT_DIR: " + ROOT_DIR)
    
    unittest.main()

No need to pass an argument when calling the test_file.py .调用test_file.py时无需传递参数。 I believe that is causing the error.我相信这是导致错误的原因。

ut-job:
  stage: test
  script:
    - echo "Unit testing ..."
    - python3 tests/test_file.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果 python 脚本抛出错误代码 1,如何使 gitlab CI 管道失败? - How to fail a gitlab CI pipeline if the python script throws error code 1? 如何在 GitLab CI 中使用 Python 和 R? - How to use Python and R in GitLab CI? 如何在gitlab(CI / CD)上使用python和mongodb - How to use python and mongodb on the gitlab (CI/CD) 如何在 python 中使用 Jenkins CI 管道代码 - How to use Jenkins CI pipeline code in python 如何将带有请求的env变量从python脚本传递给gitlab ci? - How to pass env variables from python script to gitlab ci with requests? Tox InterpreterNotFound Gitlab-CI 管道 - Tox InterpreterNotFound Gitlab-CI Pipeline 如何在不使用venv的情况下在Gitlab CI / CD中缓存python依赖项? - How to cache python dependecies in Gitlab CI/CD without using venv? 如何使用gitlab-ci来管理相互依赖的车轮的测试/构造 - How to use gitlab-ci to manage test/construction of interdependent wheels 如何在 Gitlab CI/CD 管道中使用 Docker-Compose 运行 API 端点测试 - How to run API endpoints tests with Docker-Compose in Gitlab CI/CD pipeline 如何在 gitlab CI/CD 管道中使用 pytest 测试 flask 应用程序时忽略某些脚本? - How to ignore certain scripts while testing flask app using pytest in gitlab CI/CD pipeline?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM