识别和打开外部文件

识别和打开外部文件

本文介绍了如何在测试期间让 Travis-CI 识别和打开外部文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近几次失败的构建在我的计算机上运行良好,但我无法让它们通过 travis.问题来自测试(和其他类似操作)中的几行:https://github.com/garth5689/pyd2l/blob/master/test/pyd2l_test.py#L15-L20

The last few failing builds pass fine on my computer, but I am having trouble getting them to pass on travis. The problem is coming from there few lines in the tests (and other similar operations): https://github.com/garth5689/pyd2l/blob/master/test/pyd2l_test.py#L15-L20

在我的测试中,由于我有复杂的数据要测试,我已经腌制了这些数据,以便轻松地再次打开它,而不必每次测试时都访问我多次抓取的网站.(随意讨论这种测试策略的优点,但这不是问题的主题.)

In my tests, since I have complicated data to test against, I have pickled this data to easily open it back up again and not have to hit the website I'm scraping multiple times every time I test. (Feel free to discuss the merits of this test strategy, but that's not the topic of the question.)

class PyD2LMethodsTest(unittest.TestCase):

    def setUp(self):
        with open('./soup_1899_pickle.pkl', 'rb') as soup_pickle:
            self.soup = pickle.load(soup_pickle)
        with open('./soup_1899_details_pickle.pkl', 'rb') as details_pickle:
            self.test_details = pickle.load(details_pickle)
        with open('./test_Match_1899_pickle.pkl', 'rb') as test_Match_pickle:
            self.test_match = pickle.load(test_Match_pickle)

这会导致我的 travis 构建中出现以下一系列错误:

This results in the following series of errors in my travis build:

======================================================================
ERROR: test_calculate_reward_rounded_ceil (pyd2l_test.MatchTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/travis/build/garth5689/pyd2l/test/pyd2l_test.py", line 70, in setUp
    with open('./test_Match_1899_pickle.pkl', 'rb') as test_Match_pickle: self.test_= pickle.load(
FileNotFoundError: [Errno 2] No such file or directory: './test_Match_1899_pickle.pkl'

这是目录结构

pyd2l (repo)
|-- pyd2l
|   |-- (actual source files)
|
+-- test
    |-- pyd2l_test.py
    |-- soup_1899_details_pickle.pkl
    |-- soup_1899_pickle.pkl
    |-- soup_404_pickle.pkl
    +-- test_Match_1899_pickle.pkl

测试在我的本地机器上运行良好.(我在测试期间将 ./ 添加到文件名中,看看我是否可以让它自己工作.我认为环境变量中可能有一些我需要使用的东西,但我不确定.也有可能 travis 不允许酸洗/取消酸洗,但我找不到任何相关的东西.感谢任何帮助.

The tests all run fine on my local machine. (I added the ./ to the file name during my testing to see if I could get it to work on my own. I think there might be something in the environment variables that I need to work with, but I'm not sure. There might also be the possibility that travis doesn't allow pickling/unpickling, but I couldn't find anything to that effect. Any help is appreciated.

Travis 构建:https://travis-ci.org/garth5689/pyd2l/builds

推荐答案

./ 是多余的;该文件的路径已经相对于您当前的工作目录.

./ is redundant; the path to the file is already relative to your current working directory.

问题是您希望它相对于您的测试目录,因此:

The problem is that you want it to instead be relative to your test directory, so:

import os
# ...

with open(os.path.join(os.path.dirname(__file__), 'soup_1899_pickle.pkl'), 'rb') as soup_pickle:

这篇关于如何在测试期间让 Travis-CI 识别和打开外部文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!