51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 708|回复: 0
打印 上一主题 下一主题

[资料] 文件引用问题之单元测试

[复制链接]
  • TA的每日心情
    无聊
    11 小时前
  • 签到天数: 943 天

    连续签到: 2 天

    [LV.10]测试总司令

    跳转到指定楼层
    1#
    发表于 2023-5-22 13:18:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    问题的产生
      最近在写单元测试时,发现一个文件路径引用的问题。我先把背景说一下,看大家能不能发现这个问题。
      我在本地写单元测试时,数据源文件用了上一级目录内的一个文件,通过相对路径的方式引用的,用IDE跑测试可以通过。当我把代码提交到线上跑集成测试时,测试不通过,原因是找不到对应的数据源文件。为什么会这样呢?
      相对路径是相对于运行环境说的,本地的IDE中运行环境的路径与线上机器运行环境的路径不一致。这就导致相对路径指向了不同的绝对路径上去了。
      那么这个问题怎么解决呢?
      问题的解决方案
      依赖代码源文件路径
      因为数据源文件与测试代码的相对路径是固定的,取得测试代码在运行时的路径,这样就可以通过测试代码的路径定位出数据源文件的路径。下面举例说明在C++,Java,Python中是怎么实现的:
      C++通过__FILE__宏来获取代码文件的路径,据此可得到其他相对路径。
      #include <iostream>
      #include <filesystem>
      int main() {
          // Get the full path to the current source file
          std::string sourcePath = __FILE__;
          // Get the directory of the source file
          std::filesystem::path sourceDirectory = std::filesystem::path(sourcePath).parent_path();
          // Construct the relative path to the file
          std::filesystem::path relativePath = "path/to/file.txt";
          // Construct the full path to the file
          std::filesystem::path filePath = sourceDirectory / relativePath;
          // Use the file path in your code
          std::cout << filePath.string() << std::endl;
          return 0;
      }


      Java 通过虚拟机定位class类的路径。
      import java.nio.file.Paths;
      public class MyClass {
          public static void main(String[] args) {
              // Get the path to the current class file
              String classPath = MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
              // Get the directory of the class file
              String classDirectory = Paths.get(classPath).getParent().toString();
              // Construct the relative path to the directory
              String relativePath = "path/to/directory";
              // Construct the full path to the directory
              String directoryPath = Paths.get(classDirectory, relativePath).toString();
          }
      }


      Python做法与C++类似,通过内置的__file__变量。
      import os
      # Get the directory of the currently executing assembly
      assembly_directory = os.path.dirname(os.path.abspath(__file__))
      # Construct the relative path to the file
      relative_path = "path/to/file.txt"
      # Construct the full path to the file
      file_path = os.path.join(assembly_directory, relative_path)


      最后我问了ChatGPT,在单元测试中,在引用文件时还要注意什么问题
      在编写单元测试时,引用文件的常见问题包括:
      1. 文件路径问题:确保在测试代码中使用相对路径时使用相对于测试项目而不是当前工作目录的相对路径。通常最好使用绝对路径或从环境变量中获取路径。
      并发问题:如果测试使用同一个文件进行读写,确保测试代码正确处理并发访问。可以使用2. 锁定、读写锁或其他同步机制来避免并发问题。
      3. 依赖项问题:确保在测试代码中正确处理文件依赖项。如果测试代码依赖于某些文件,那么在测试运行之前应该确保这些文件存在,并在测试结束时进行清理。
      4. 数据问题:确保测试使用的文件包含正确的数据。在编写测试时,应该使用不同的数据集来测试各种情况,例如测试空文件、小文件和大文件等。
      5. 兼容性问题:确保测试代码在不同操作系统和文件系统中正确运行。文件路径和分隔符可能因操作系统而异。在编写测试代码时,应该在不同的操作系统和文件系统中进行测试。
      6. 持久化问题:确保测试代码不会在测试结束时留下残留文件。在测试结束时,应该清理所有已创建的文件。

    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-5-7 23:03 , Processed in 0.062324 second(s), 22 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表