用值填充 MySQL 数据库

发布于 2024-10-08 16:09:14 字数 270 浏览 0 评论 0原文

我的笔记本电脑上有一个本地安装的 MySQL 服务器,我想使用其中的信息进行单元测试,所以我想创建一个脚本来自动生成所有数据。我正在使用 MySQL Workbench,它已经生成了表(从模型)。是否可以使用它或其他工具来创建自动脚本来填充数据?

编辑:我现在发现我不清楚。我确实有对单元测试有意义的数据。当我说“自动生成所有数据”时,我的意思是该工具应该获取我今天在本地数据库中拥有的有意义的数据,并创建一个脚本以在其他开发人员的数据库中生成相同的数据

I have a locally installed MySQL server on my laptop, and I want to use the information in it for a unit-test, so I want to create a script to generate all the data automatically. I'm using MySQL Workbench which already generates the tables (from the model). Is it possible to use it, or another tool, to create an automatic script to populate it with data?

EDIT: I see now that I wasn't clear. I do have meaningful data for the unit test. When I said "generate all the data automatically", I meant the tool should take the meaningful data I have in my local DB today and create a script to generate the same data in other developers' DBs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谜兔 2024-10-15 16:09:14

最有用的单元测试是那些反映您期望或在实践中看到的数据的单元测试。泵入充满随机位的模式并不能替代精心设计的测试数据。正如 @McWafflestix 所建议的 mysqldump 是一个有用的工具,但如果您想要更简单的东西,请考虑使用 使用 INFILE 加载数据,从 CSV 填充表格。

其他一些需要考虑的事情:

  1. 使用处于已知状态的数据库进行测试。将所有数据库交互单元测试包装在始终回滚的事务中。
  2. 使用 dbunit 达到相同的目的。

更新

如果您在Java环境中,dbUnit是一个很好的解决方案:

  1. 您可以通过其API以XML格式导入和导出数据,这将解决从您的计算机到其他成员的问题在你的团队中。
  2. 它旨在恢复数据库状态。因此,它会在执行测试之前对数据库进行快照,然后在测试结束时进行恢复。因此测试没有副作用(即它们不会永久更改数据)。

The most useful unit tests are those that reflect data you expect or have seen in practice. Pumping your schema full of random bits is not a substitute for carefully crafted test data. As @McWafflestix suggested mysqldump is a useful tool, but if you want something simplier, consider using LOAD DATA with INFILE, which populates a table from a CSV.

Some other things to think about:

  1. Test with a database in a known state. Wrap all your database interaction unit tests in transactions that always roll back.
  2. Use dbunit to achieve the same end.

Update

If you're in a Java environment, dbUnit is a good solution:

  1. You can import and export data in an XML format through its APIs, which would solve the issue of going from your computer to other members on your team.
  2. It's designed to restore database state. So it snapshots the database before tests are executed and then restores at then end. So tests are side effect free (i.e. they don't permanently change data).
你穿错了嫁妆 2024-10-15 16:09:14

您可以使用默认值填充(如果已定义)

CREATE TABLE #t(c1 int DEFAULT 0,c2 varchar(10) DEFAULT '-')
GO
--This insert 50 rows in table
INSERT INTO #t( c1, c2 )
DEFAULT VALUES
GO 50

SELECT * FROM #t
DROP TABLE #t

You can populate with defaults (if defined)

CREATE TABLE #t(c1 int DEFAULT 0,c2 varchar(10) DEFAULT '-')
GO
--This insert 50 rows in table
INSERT INTO #t( c1, c2 )
DEFAULT VALUES
GO 50

SELECT * FROM #t
DROP TABLE #t
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文