【问题标题】:Ignoring Test Files with Nose/unittest使用 Nose/unittest 忽略测试文件
【发布时间】:2018-12-15 20:48:25
【问题描述】:

我有两个测试文件。

一个是标准的测试用例。另一个有硒测试。我有一个 docker-compose 文件设置来运行 selenium 测试及其相关服务。

测试在 bitbucket 管道中运行。首先,它使用标准的 TestCases 运行测试。然后在下一步中,它使用 docker-compose 运行包含 selenium 的测试,因为它需要 compose 来设置 selenium 服务。

我遇到的问题是,当没有任何硒容器在运行时,它试图在第一步中运行硒测试。

我在用于第一个测试部分的 Django 设置文件中输入了这个:

NOSE_ARGS = [
    '--exclude=(__init__.py)',
    '--exclude=(../tests/selenium_tests.py)',
    '--exclude=(selenium_tests.py)',
    '--with-coverage',
    '--cover-package=sasite',
    '--with-xunit',
    '--xunit-file=test-results/nose/noseresults.xml',
    '--verbosity=3',
    '--cover-xml',
    '--cover-xml-file=test-results/nose/noseresults.xml',
    '--id-file=test-results/nose/noseids'
]

在我的第二个设置中:

NOSE_ARGS = [
    '--exclude=(__init__.py)',
    '--with-coverage',
    '--cover-package=sasite',
    '--with-xunit',
    '--xunit-file=test-results/nose/noseresults.xml',
    '--verbosity=3',
    '--cover-xml',
    '--cover-xml-file=test-results/nose/noseresults.xml',
    '--id-file=test-results/nose/noseids'
]

如您所见,我想排除第一部分的硒测试,并将它们用于第二部分。但即使我在nose_args 中排除了它,它仍然会在第一部分运行硒测试。

这是我的 bitbucket-pipelines.yml:

   prod-final:
      - step:
          name: Build image, run tests, and push to Docker Hub
          caches:
            - docker
          services:
            - docker
          script:
            # Set $DOCKER_HUB_USERNAME and $DOCKER_HUB_PASSWORD as environment variables in repository settings
            - export DOCKER_HUB_USERNAME=X
            - export DOCKER_HUB_PASSWORD=XXX

            # build the Docker image (this will use the Dockerfile in the root of the repo)
            - docker build -t meleiyu/sawebsite-redesign -f Dockerfile.imagetest .
            # run tests
            - docker run meleiyu/sawebsite-redesign
            # tag Docker image
            - docker tag meleiyu/sawebsite-redesign meleiyu/sawebsite-redesign:latest
            # authenticate with the Docker Hub registry
            - docker login --username $DOCKER_HUB_USERNAME --password $DOCKER_HUB_PASSWORD
            # push the new Docker image to the Docker registry
            - docker push meleiyu/sawebsite-redesign:latest      
      - step:
          name: Run second level of testing with BrowserStack
          image: docker:stable
          trigger: manual          
          caches:
            - docker
          services:
            - docker
          script:
            # Test production setup with compose
            - apk add --no-cache py-pip bash
            - pip install --no-cache docker-compose
            - docker-compose -f docker-compose.test.yml up -d --build --exit-code-from app
      - step:
          # set AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_DEFAULT_REGION as environment variables
          name: Deploy to AWS
          deployment: staging   # set to test, staging or production
          trigger: manual  # uncomment to have a manual step
          image: atlassian/pipelines-awscli
          script:
            - aws deploy push --application-name sawebsite-redesign --s3-location s3://<S3 bucket>/<s3-key> --ignore-hidden-files
            - aws deploy create-deployment --application-name sawebsite-redesign --s3-location bucket=<s3-bucket>,key=<s3-key>,bundleType=zip --deployment-group-name <deployment-group>

definitions:
  services:
    docker:
      memory: 3072

Dockerfile.imagetest 用于第一部分,并具有排除硒测试文件的设置。

Dockerfile.test 和 docker-compose.test.yml 用于第二部分,不排除 selenium 测试文件。

我的配置/设置有意义吗?我是否在nose_args 中正确排除了文件?因为正如我所说,它在第一部分运行硒测试时不应该这样做。

我们将不胜感激。

谢谢

编辑:

我现在的 NOSE_ARGS:

不要忽略任何测试:

NOSE_ARGS = [
    '--exclude=(__init__.py)',
    '--with-coverage',
    '--cover-package=sasite',
    '--with-xunit',
    '--xunit-file=test-results/nose/noseresults.xml',
    '--verbosity=3',
    '--cover-xml',
    '--cover-xml-file=test-results/nose/noseresults.xml',
    '--id-file=test-results/nose/noseids'
]

排除硒测试:

NOSE_ARGS = [
    '--exclude=(__init__.py)',
    '--exclude=(../tests/selenium_tests.py)',
    '--exclude=(selenium_tests.py)',
    '--ignore=(selenium_tests.py)',
    '--ignore=(../tests/selenium_tests.py)',
    '--ignore-files=../tests/selenium_tests.py',    
    '--ignore-files=selenium_tests.py',    
    '--with-coverage',
    '--cover-package=sasite',
    '--with-xunit',
    '--xunit-file=test-results/nose/noseresults.xml',
    '--verbosity=3',
    '--cover-xml',
    '--cover-xml-file=test-results/nose/noseresults.xml',
    '--id-file=test-results/nose/noseids'
]

【问题讨论】:

    标签: python django selenium docker docker-compose


    【解决方案1】:

    基于https://nose.readthedocs.io/en/latest/usage.html#cmdoption-e --exclude 参数应该是一个正则表达式。

    您尝试过类似--exclude=selenium 的方法吗?

    编辑

    假设您想忽略项目中有一个测试tests/integration/test_integration.py。跑步

    nosetests -v --exclude=integration
    

    nosetests -v --ignore-files=test_integration.py
    

    成功了(即除了test_integration.py 中的测试之外的所有测试都被执行)。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-30
    • 1970-01-01
    • 2014-06-06
    • 2012-11-11
    • 2023-03-05
    • 1970-01-01
    • 2019-11-16
    • 2021-11-20
    相关资源
    最近更新 更多